HDOJ 1102 Constructing Roads.(Prim算法实现)

来源:互联网 发布:php全站搜索 编辑:程序博客网 时间:2024/05/23 15:45

Constructing Roads

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


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   |   We have carefully selected several similar problems for you:  1301 1162 1875 1863 1325 
 







题意:输入n,给出一个n*n的矩阵(从1到n编号)。第i行j列的元素表示城市i与j之间的距离。再给出Q,随后Q行表示两城市之间的道路已建成。求所有城市连通所需要建的最短长度。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<set>#include<queue>#include<stack>#include<vector>#include<algorithm>using namespace std;const int inf = 1<<30;int maz[105][105],dis[105],n;bool flag[105];void prim(){    int i,j,pos,t;    for(i=1;i<=n;i++)        dis[i] = inf;    for(i=1;i<=n;i++)        flag[i] = false;    //标记是否访问    dis[1] = 0;    for(i=1;i<=n;i++){        t =inf;        for(j=1;j<=n;j++){  //找当前节点最短路            if(!flag[j]&&dis[j]<t){                t = dis[j];                pos = j;    //记录最短路下标            }        }        flag[pos] = true;   //不再访问        for(j=1;j<=n;j++){            if(!flag[j]&&dis[j]>maz[pos][j]){                dis[j] = maz[pos][j];   //将当前最短路以外的路长都改为最短长度            }        }    }    int mount = 0;    for(i=1;i<=n;i++)        mount += dis[i];    cout<<mount<<endl;}int main(){    int i,j;    while(cin>>n){  //题目没说有多组数据 因为这个贡献了好几个WA... QAQ        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                scanf("%d",&maz[i][j]);     //建图            }        }        int m,a,b;        cin>>m;        while(m--){            scanf("%d%d",&a,&b);            maz[a][b] = maz[b][a] =0; //将已建成公路造价(长度)改为0        }        prim();    }}







原创粉丝点击