Source: http://poj.org/problem? Id = 2421
Constructing roads
| Time limit:2000 ms |
|
Memory limit:65536 K |
| Total submissions:19645 |
|
Accepted:8193 |
Description
There are n versions, which are numbered from 1 to n, And You shoshould build some roads such that every two versions ages 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 versions ages and your job is the build some roads such that all the versions 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 ages. 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 shocould 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 shoshould output a line contains an integer, which is the length of all the roads to be built such that all the versions are connected, and this value is minimum.
Sample Input
30 990 692990 0 179692 179 011 2
Sample output
179
Source
PKU monthly, KICC
Question: omitted.
Question: Graph Theory, Minimum Spanning Tree, and prim. This question is basically no different from that of poj 1258. It just adds several edges to the question in the initial stage. These edges only need to assign their weights to zero ~~
AC code:
#include<iostream>#include<cstring>const int INF=0x3fffffff; using namespace std;int map[105][105],t,n,x,y,wage=0,pb;bool p[105];void Prim(){int tempx,tempy;while(pb--){int min=INF;for(int i=1;i<=n;i++)if(p[i]){for(int j=1;j<=n;j++){if(!p[j]&&map[i][j]<min){tempx=i;tempy=j;min=map[i][j];}}}p[tempy]=true;wage+=min;}}int main(){memset(p,0,sizeof(p));p[1]=true;cin>>n;pb=n-1;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>map[i][j];cin>>t;while(t--){cin>>x>>y;map[x][y]=0;map[y][x]=0;}Prim();cout<<wage<<endl;return 0;}
Poj 2421 constructing roanalyticdb