You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.
Given n and each cij , find the cheapest way to connect computers.
Input
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. ThenT test cases follow.
The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers i and j(cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii = 0, 1 <= i, j <= n.
Output
For each test case, if you can connect the computers together, output the method in in the following fomat:
i1 j1 i1 j1 ......
where ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line. If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.
Sample Input
2 3 0 2 3 2 0 5 3 5 0 2 0 0 0 0
Sample Output
1 2 1 3 -1
Hints:
A solution A is a line of p integers: a1, a2, ...ap.
Another solution B different from A is a line of q integers: b1, b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= p, r <= q) such that ai = bi for all 0 < i < r and ar < br
OR
(2) p < q and ai = bi for all 0 < i <= p
#include<iostream> #include<algorithm> #include<string> #include<map> #include<string.h> #include<vector> #include<cmath> #include<stdlib.h> #include<cstdio> #define ll long long #define MAXN 250005 using namespace std; long long fa[MAXN]; long long ranks[MAXN]; struct edge{ long long u,v,cost; }; edge es[MAXN]; long long V,E; //V:顶点数 E:边数 int u; pair<int,int> p[MAXN]; int cmp2(pair<int,int>a,pair<int,int>b){ if(a.first!=b.first) return a.first<b.first; else return a.second<b.second; } bool comp(const edge&e1,const edge&e2){ if (e1.cost != e2.cost) return e1.cost<e2.cost; if (e1.u != e2.u) return e1.u < e2.u; else return e1.v < e2.v; } long long getfather(long long v){ if (fa[v] == v) return v; else return fa[v]=getfather(fa[v]); } void k(){ for(long long i=0;i<V;i++){ fa[i]=i; } //long long res = 0; for(long long i=0;i<E;i++){ edge e = es[i]; long long x = getfather(e.u); long long y = getfather(e.v); if(x!=y){ fa[x]=y; p[++u].first=min(e.u,e.v)+1; //注意 p[u].second=max(e.u,e.v)+1; // res+=e.cost; } } // return res; //最少花费 } int main(){ long long t; cin>>t; while(t--){ u=-1; cin >> V; long long c; int w=-1; for(int i=0;i<V;++i){ for(int j=0;j<V;++j){ cin>>c; if(c!=0&&j>i){ es[++w].v=i; es[w].u=j; es[w].cost=c; } } } E=w+1; sort(es,es + E,comp); k(); int kk=getfather(0); for(int i=1;i<V;++i){ if(getfather(i)!=kk){ u=-1; break; } } if(u==-1) cout<<-1<<endl; else{ sort(p,p+u+1,cmp2); //最后还要排一次序 for(int i=0;i<u;++i) cout<<p[i].first<<" "<<p[i].second<<" "; cout<<p[u].first<<" "<<p[u].second<<endl; } } return 0; } /* 1 4 0 5 0 0 5 0 0 0 0 0 0 100 0 0 100 0 */
In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, A, B, K (0 <= A, B < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.
Output
For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.
Sample Input
1 3 3 0 1 5 0 2 0 1 2 9
Sample Output
5
prim算法。。。
#include<iostream> #include<algorithm> #include<string> #include<map> #include<string.h> #include<vector> #include<cmath> #include<stdlib.h> #include<cstdio> #define ll long long using namespace std; int cost[510][510]; int mincost[200010]; bool used[200010]; int V; int prim(){ for(int i=0;i<V;++i){ mincost[i]=1000000000; used[i]=false; } mincost[0]=0; int res=0; while(true){ int v=-1; for(int u=0;u<V;u++){ if(!used[u]&&(v==-1||mincost[u]<mincost[v])) v=u; } if(v==-1) break; used[v]=true; res+=mincost[v]; for(int u=0;u<V;u++){ mincost[u]=min(mincost[u],cost[v][u]); } } return res; } int main(){ int t; cin>>t; while(t--){ int m,a,b,c; cin>>V>>m; for(int i=0;i<V;++i){ for(int j=0;j<V;++j){ cost[i][j]=1000000000; } } while(m--){ cin>>a>>b>>c; cost[a][b]=c; cost[b][a]=c; } cout<<prim()<<endl; } return 0; }