标签:
There are multiple test cases.
The first line contains two integers N and M indicating the number of vertices and edges in the gragh.
The next M lines, each line contains three integers a, b, l and c indicating there are an edge with l length and c cost between a and b.
1 <= N <= 10,000
1 <= M <= 100,000
1 <= a, b <= N
1 <= l, c <= 10,000
4 5 1 2 1 1 2 3 1 1 3 4 1 1 1 3 1 2 2 4 1 3
3 3
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 500010; 5 struct arc{ 6 int u,v,length,cost; 7 bool operator<(const arc &rhs)const{ 8 if(length == rhs.length) return cost < rhs.cost; 9 return length < rhs.length; 10 } 11 }e[maxn]; 12 int uf[maxn]; 13 int Find(int x){ 14 if(x != uf[x]) uf[x] = Find(uf[x]); 15 return uf[x]; 16 } 17 int main(){ 18 int n,m; 19 while(~scanf("%d%d",&n,&m)){ 20 for(int i = 0; i < m; ++i) 21 scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].length,&e[i].cost); 22 for(int i = 0; i <= n; ++i) uf[i] = i; 23 sort(e,e + m); 24 LL length = 0,cost = 0,cnt = 0; 25 for(int i = 0; i < m && cnt + 1 < n; ++i){ 26 int u = Find(e[i].u); 27 int v = Find(e[i].v); 28 if(u == v) continue; 29 uf[u] = v; 30 length += e[i].length; 31 cost += e[i].cost; 32 ++cnt; 33 } 34 if(cnt + 1 == n) printf("%lld %lld\n",length,cost); 35 else puts("-1 -1"); 36 } 37 return 0; 38 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4970306.html