LightOJ - 1041 Road Construction
2016-07-22 21:02
387 查看
LightOJ - 1041 Road Construction
Description There are several cities in the country, and some of them are connected by bidirectional roads. Unfortunately, some of the roads are damaged and cannot be used right now. Your goal is to rebuild enough of the damaged roads that there is a functional path between every pair of cities. You are given the description of roads. Damaged roads are formatted as "city1 city2 cost" and non-damaged roads are formatted as "city1 city2 0". In this notation city1 and city2 are the case-sensitive names of the two cities directly connected by that road. If the road is damaged, cost represents the price of rebuilding that road. Every city in the country will appear at least once in roads. And there can be multiple roads between same pair of cities. Your task is to find the minimum cost of the roads that must be rebuilt to achieve your goal. If it is impossible to do so, print "Impossible". Input Input starts with an integer T (≤ 50), denoting the number of test cases. Each case begins with a blank line and an integer m (1 ≤ m ≤ 50) denoting the number of roads. Then there will be m lines, each containing the description of a road. No names will contain more than 50 characters. The road costs will lie in the range [0, 1000]. Output For each case of input you have to print the case number and the desired result. Sample Input 2 12 Dhaka Sylhet 0 Ctg Dhaka 0 Sylhet Chandpur 9 Ctg Barisal 9 Ctg Rajshahi 9 Dhaka Sylhet 9 Ctg Rajshahi 3 Sylhet Chandpur 5 Khulna Rangpur 7 Chandpur Rangpur 7 Dhaka Rajshahi 6 Dhaka Rajshahi 7 2 Rajshahi Khulna 4 Kushtia Bhola 1 Sample Output Case 1: 31 Case 2: Impossible |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int a
;
char str
, s1
, s2
;
struct node
{
int x, y, w;
}p
;
int merg(int x,int y);
int Find(int x);
bool cmp(node a,node b)
{
return a.w<b.w;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
memset(str,0,sizeof(str));
int m, w, j, l=0;
scanf("%d", &m);
for(int i=0;i<m;i++)
{
scanf(" %s %s %d", s1, s2, &w);
for( j=0;j<l;j++)
{
if(strcmp(str[j],s1)==0)
{
p[i].x=j;
break;
}
}
if(j==l)
{
strcpy(str[l],s1);
p[i].x=l;
l++;
}
for( j=0;j<l;j++)
{
if(strcmp(str[j],s2)==0)
{
p[i].y=j;
break;
}
}
if(j==l)
{
strcpy(str[l],s2);
p[i].y=l;
l++;
}
p[i].w=w;
}
sort(p,p+m,cmp);
for(int i=0;i<l;i++)
{
a[i]=i;
}
int sum=0;
for(int i=0;i<m;i++)
{
int x=p[i].x, y=p[i].y;
if(merg(x,y)==0)
{
sum+=p[i].w;
}
}
int flag=0;
for(int i=0;i<l;i++)
{
if(a[i]==i)
{
flag++;
}
}
if(flag!=1)
{
printf("Case %d: Impossible\n",ncase++);
}
else
{
printf("Case %d: %d\n",ncase++,sum);
}
}
return 0;
}
int merg(int x,int y)
{
int t1=Find(x);
int t2=Find(y);
if(t1==t2)
{
return 1;
}
else
{
a[t1]=t2;
return 0;
}
}
int Find(int x)
{
if(x==a[x])
{
return x;
}
else
{
a[x]=Find(a[x]);
return a[x];
}
}
相关文章推荐
- SonarQube代码质量管理平台安装与使用
- J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- php各种排序
- J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- 2016 Multi-University Training Contest 2 1005 Eureka
- 自制tunnel口建虚拟专网实验
- 自制tunnel口建虚拟专网实验
- 自制tunnel口建虚拟专网实验
- R语言函数报错继续执行方法
- 获取Map中的所有value
- Handler,Looper,MessageQueue,ThreadLocal讲解以及实例
- DIY 跟据日期计算星期几
- Jquery实现文本框得到焦点的时候,文本框的焦点出现在最后!
- 树状数组 ( 基础篇 )——敌兵布阵 ( HDU 1166 )
- Android 自定义View-图片文字变色,实现酷炫LoadingView或者进度条
- S60在使用底座授权的时候,清除puk
- CodeForces 489D Unbearable Controversy of Being (搜索)
- 【C语言】实例:整齐地输出n的平方,立方
- cocos2d-x + vs2015 android游戏开发环境搭建 及 新建项目
- URL中查询字符串是什么意思