链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204
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.
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T 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.
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.
2 3 0 2 3 2 0 5 3 5 0 2 0 0 0 0
1 2 1 3 -1
比较简单的模板题目,直接上代码。
附上AC代码:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip> #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> #include <stack> #include <deque> //#pragma comment(linker, "/STACK:102400000, 102400000") using namespace std; typedef long long ll; const double pi = acos(-1.0); const double e = exp(1.0); const double eps = 1e-8; const short maxn = 105; struct edge{ short first, second; int cost; } comp[maxn*maxn], ans[maxn*maxn]; // 分别存储原来有用的边和答案所得到的边 short p[maxn]; // 存储父节点 short cnt, total, n; // 分别存储结果边数,有效边数和电脑数 short find(short x); bool cmp1(edge a, edge b); bool cmp2(edge a, edge b); void kruskal(); int main(){ ios::sync_with_stdio(false); short T; scanf("%hd", &T); while (T--){ scanf("%hd", &n); int cost; total = 0; for (short i=1; i<=n; i++) for (short j=1; j<=n; j++){ scanf("%d", &cost); if (j<=i || 0==cost) continue; // 无向图,对称的边无效,或者花费为0的边也无效 comp[total].first = i; comp[total].second = j; comp[total].cost = cost; total++; } sort(comp, comp+total, cmp1); cnt = 0; kruskal(); if (cnt != n-1){ // 不是树 printf("-1\n"); continue; } sort(ans, ans+cnt, cmp2); for (short i=0; i<cnt-1; i++) printf("%hd %hd ", ans[i].first, ans[i].second); printf("%hd %hd\n", ans[cnt-1].first, ans[cnt-1].second); } return 0; } short find(short x){ // 并查集的查找操作 return p[x]==x ? x : p[x]=find(p[x]); } bool cmp1(edge a, edge b){ if (a.cost != b.cost) return a.cost < b.cost; // 按照花费从小到大排序 if (a.first != b.first) // 以下为按字典序排序 return a.first < b.first; return a.second < b.second; } bool cmp2(edge a, edge b){ if (a.first != b.first) // 以下为按字典序排序 return a.first < b.first; return a.second < b.second; } void kruskal(void){ for (short i=1; i<=n; ++i) p[i] = i; // 初始化父节点 for (short i=0; i<total; ++i){ short p1 = find(comp[i].first); short p2 = find(comp[i].second); if (p1 != p2){ // 不在同一个连通块中,满足条件,加入结果 ans[cnt++] = comp[i]; p[p1] = p2; } } }