HDU 3371 Prim或kruskal实现

来源:互联网 发布:oracle sql循环语句 编辑:程序博客网 时间:2024/06/15 14:16

Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16619 Accepted Submission(s): 4256

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.

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

刚开始用WA了,因为这里可能两个城市可能有多条直达的路,
例如:
1 2 3
1 2 4
这时候当然选 1 2 3 加个判断
TLE
然后把init()换成memset()
但是memset初始化慎用 :INF超过一定值会出错的,建议init()初始化,
过了

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define INF 200000const int MAXN=550;int mp[MAXN][MAXN],dis[MAXN];bool vis[MAXN];int T,n,m,k;void init(){    int i,j;    for(i=1;i<=n;i++){        for(j=1;j<=n;j++){           if(i==j) mp[i][j]=0;   //对a[][]进行初始化,一般都要;           else mp[i][j]=INF;        }    }}int prim(int n){    memset(vis,0,sizeof(vis));    int mi,pos,sum;    sum=0;    for(int i=1;i<=n;i++)dis[i]=mp[1][i];    vis[1]=1;    for(int i=1;i<n;i++){        mi=INF;        for(int j=1;j<=n;j++){            if(!vis[j]&&dis[j]<mi){                mi=dis[j];                pos=j;            }        }        if(mi==INF)return -1;        sum+=mi;        vis[pos]=1;        for(int j=1;j<=n;j++){            if(!vis[j]&&dis[j]>mp[pos][j]){                dis[j]=mp[pos][j];            }        }    }    return sum;}int main(){    int a,b,l,x,t1,t2;    scanf("%d",&T);    while(T--){        scanf("%d%d%d",&n,&m,&k);        memset(mp,INF,sizeof(mp));        while(m--){            scanf("%d%d%d",&a,&b,&l);            if(l<mp[a][b])                mp[a][b]=mp[b][a]=l;        }        while(k--){            scanf("%d",&x);            int b[MAXN];            for(int i=1;i<=x;i++)scanf("%d",b+i);            for(int i=1;i<x;i++)                for(int j=i+1;j<=x;j++)                    mp[b[i]][b[j]]=mp[b[j]][b[i]]=0;        }        int ans=prim(n);        printf("%d\n",ans);    }    return 0;}

kruskal的实现:

#pragma comment(linker, "/STACK:102400000,102400000")#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>#include<map>using namespace std;#define LL long long#define INF 0x3f3f3f3fconst int N=550;//点的个数const int M=30000;//边的条数int F[N];struct Edge{    int u,v,w;}edges[M];int cmp(Edge a,Edge b){    return a.w<b.w;}int find(int x){    return F[x]==x?x:F[x]=find(F[x]);}void Union(int x,int y){    int t1=find(x);    int t2=find(y);    if(t1!=t2)F[t1]=t2;}int Kruscal(int n,int m){    int ans=0,tot=0;    sort(edges+1,edges+m+1,cmp);    for(int i=1;i<=m;i++){        int x=find(edges[i].u),y=find(edges[i].v);        if(x!=y){ans+=edges[i].w;F[x]=y;tot++;}    }    int tt=0;    for(int i=1;i<=n;i++){        if(F[i]==i)tt++;        if(tt>1)return -1;    }    return ans;}int main(){    int T,n,m,k,t;    scanf("%d",&T);    while(T--){        scanf("%d%d%d",&n,&m,&k);        for(int i=1;i<=m;i++){            scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);        }        for(int i=1;i<=n;i++)F[i]=i;        while(k--){            int t1,t2;            scanf("%d%d",&t,&t1);            t--;            while(t--){                scanf("%d",&t2);                Union(t1,t2);                t1=t2;            }        }        //for(int i=1;i<=n;i++)printf("%d->%d\n",i,F[i]);        printf("%d\n",Kruscal(n,m));        //for(int i=1;i<=n;i++)printf("%d->%d\n",i,F[i]);    }    return 0;}
0 0
原创粉丝点击