Connect the Cities
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.
Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
Sample Input
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
Sample Output
1
Author
Source
HDOJ Monthly Contest – 2010.04.04
G++ 超时,c++不超时。很是奇怪和无语!!!!
从测试结果观察的,不知道对不对:
vc++ 可以跨界使用定义过的局部变量,但是对函数返回值类型必须向对应!速度貌似比G++快!
G++ 不可以跨界使用定义过的局部变量,但是对函数返回值没有要求,甚至可以没有当作有。
这题 n (3 <= n <=500),m (0 <= m <= 25000),应该算是稠密图了,推荐使用prim算法;
但是kruskal也是可以过的。
kruskal 其实更加适合相对稀疏的图,他运用了贪心(排序上体现)+并查集的思想,所以编程的思路还是比较清晰的。
结构体的快排:sort(node,nude+n,cmp);
并查集的优化:我想路径压缩大家都是知道的,但是另外的将深度小的树合并到深度大的树,这样会省100多ms(这题限1000ms),表示测试数据有点狠。
这题抽象出来的模型是:n 个点用很多条边连成树,连成环的边可以不计。判断能不能连成树,可以的话,给出最小边的权和。
知道了2个方法,1.并查集的思想,将已经连起来的点放到相应的集合中,再用kruscal模版
2.将已经相连点之间的边权赋值为0,再用prim或者kruscal模版
kruacal:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 501
struct Node{int x,y,z;
};
Node node[N*50];
int father[N],rank[N],sum,num;
void init(int n){num=n;sum=0;for(int i=1;i<=n;i++)father[i]=i,rank[i]=1;
}
int cmp(Node a,Node b){return a.z<b.z;
}
int find(int x){return father[x]==x?x:find(father[x]);
}
int combine(int a,int b,int v){int x=find(a),y=find(b);if(x==y)return 0;else{if(rank[x]>rank[y])father[y]=x;else { if(rank[x]==rank[y]) { rank[y]++; } father[x]=y; } sum+=v;num--;}
}
int main(){int n,m,k,i,j,a,b,v,t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);init(n);for(i=0;i<m;i++)scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z);for(i=0;i<k;i++){scanf("%d%d",&v,&a);for(j=0;j<v-1;j++){scanf("%d",&b);combine(a,b,0);}}if(num!=1) {sort(node,node+m,cmp);for(i=0;i<m;i++){combine(node[i].x,node[i].y,node[i].z);if(num==1)break;}}if(num==1)printf("%d\n",sum);elseprintf("-1\n");}
// scanf("%d",&t);
}
prim:
#include<stdio.h>
#include<string.h>
#define N 501
#define INF 200000
int dis[N][N],vis[N],mins[N],sum,ans;
void init(int n){sum=0;ans=0;memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++)mins[i]=INF;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) dis[i][j]=INF;}
}int prim(int n,int m){int i,j,k=1;mins[1]=0;for(i=1;i<=n;i++){int min=INF;for(j=1;j<=n;j++){if(!vis[j]&&mins[j]<min){min=mins[j];k=j;}}sum+=min;vis[k]=1;for(j=1;j<=n;j++){if(!vis[j]&&dis[k][j]<mins[j])mins[j]=dis[k][j];}}for(i=1;i<=n;i++)if(!vis[i]){ans=1;break;}
}
int main(){int t,i,j,k,a,b,v,m,n;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);init(n);for(i=0;i<m;i++){scanf("%d%d%d",&a,&b,&v);if(v<dis[a][b]){dis[a][b]=v;dis[b][a]=v;} }for(i=0;i<k;i++){scanf("%d%d",&v,&a);for(j=0;j<v-1;j++){scanf("%d",&b);dis[a][b]=0;dis[b][a]=0;}}// for(i=1;i<=n;i++){// for(j=1;j<=n;j++)// printf("%8d ",dis[i][j]);
// printf("\n");// }prim(n,m); if(ans)printf("-1\n");else printf("%d\n",sum);}}