hdu_1102 Constructing Roads

来源:互联网 发布:java nextline用法 编辑:程序博客网 时间:2024/06/10 05:46


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19786    Accepted Submission(s): 7563


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. 
 

Sample Input
30 990 692990 0 179692 179 011 2
 

Sample Output
179
 

Source
kicc
 

Recommend
Eddy

复习一下最小生成树

先用prime算法

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int N,Q;int m[110][110];int dis[110];int visit[110];int prim(){    int i,j;    for(i=1; i<=N; i++) dis[i]=inf;    dis[1]=0;    for(j=1; j<=N; j++)    {        int min=inf,pos;        for(i=1; i<=N; i++)        {            if(dis[i]<min&&!visit[i])            {                min=dis[i];                pos=i;            }        }        visit[pos]=1;        for(i=1; i<=N; i++)        {            if(!visit[i]&&dis[i]>m[pos][i]&&m[pos][i]!=inf)            {                dis[i]=m[pos][i];            }        }    }    int res=0;    for(i=1; i<=N; i++)    {        res+=dis[i];    }    return res;}int main(){    int a,b,i,j;    while(~scanf("%d",&N))    {        mem(visit,0);        for(i=1; i<=N; i++)            for(j=1; j<=N; j++)                Si(m[i][j]);        Si(Q);        while(Q--)        {            Si(a),Si(b);            m[a][b]=m[b][a]=0;        }        Pi(prim());    }    return 0;}

用上并查集的kruskal算法

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int N,Q;int dis[110];int visit[110];int cnt=0;int ans=0;struct Edge{    int u,v,cost;} edge[10010];int father[110];bool cmp(Edge a,Edge b){    return a.cost<b.cost;}void makeSet(){    for(int i=1; i<=N; i++)    {        father[i]=i;    }}int findSet(int x){    while(x!=father[x])    {        x=father[x];    }    return x;}int Kruskal(){    sort(edge,edge+cnt,cmp);    for(int i=0; i<cnt; i++)    {        int fx=findSet(edge[i].u);        int fy=findSet(edge[i].v);        if(fx==fy)continue;        ans+=edge[i].cost;        father[fy]=fx;    }}int main(){    int u,v,i,j;    while(~scanf("%d",&N))    {        cnt=0;        ans=0;        mem(visit,0);        for(i=1; i<=N; i++)            for(j=1; j<=N; j++)            {                Si(edge[cnt].cost);                edge[cnt].u=i;                edge[cnt].v=j;                cnt++;            }        makeSet();        Si(Q);        while(Q--)        {            Si(u),Si(v);            int fx=findSet(u);            int fy=findSet(v);            father[fy]=fx;        }        Kruskal();        Pi(ans);    }    return 0;}


0 0