Description
Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.
Input
The first line of input is the number of test case.
The first line of each test case contains three integers, N, M and R.
Then R lines followed, each contains three integers xi, yi and di.
There is a blank line before each test case.
1 ≤ N, M ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000
Output
For each test case output the answer in a single line.
Sample Input
2
5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781
5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133
Sample Output
71071
54223
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;
const int INF = 1 << 30;
const int sum = 10000;
const int MAXN = 10010 * 2;
const int MAXR = 50010 * 2;
struct edge{
int to,cost;
};
vector<edge> es[MAXR];
typedef pair<int,int> P;
priority_queue<P,vector<P>,greater<P> > heap;
int n,m,r,ans,d[MAXN],vis[MAXN];
int main(){
int T,i,j;
int x,y,c;
scanf("%d",&T);
for(int p = 0;p < T; ++p){
scanf("%d%d%d",&n,&m,&r);
for(i = 0;i < m + n; ++i)
es[i].clear();
memset(vis,0,sizeof(vis));
for(i = 0;i < r; ++i){
scanf("%d%d%d",&x,&y,&c);
edge e;
e.cost = sum - c;
e.to = y + n;
es[x].push_back(e);
e.to = x;
es[y + n].push_back(e);
}
ans = sum;
fill(d,d + n + m,INF);
d[0] = 0;
heap.push(P(0,0));
vis[0] = 1;
int cnt = 1;
while(1){
while(!heap.empty()){
P p = heap.top();
heap.pop();
int pf = p.first;
int v = p.second;
if(pf > d[v])
continue;
if(!vis[v]){
ans += pf;
++cnt;
vis[v] = 1;
}
for(i = 0;i < es[v].size(); ++i){
edge e = es[v][i];
if(!vis[e.to] && e.cost < d[e.to]){
d[e.to] = e.cost;
heap.push(P(d[e.to],e.to));
}
}
}
if(cnt == n + m)
break;
else{
for(i = 0;i < n + m; ++i){
if(!vis[i]){
vis[i] = 1;
++cnt;
heap.push(P(0,i));
ans += sum;
d[i] = 0;
break;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
方法一:Prim,注意非连通。(麻烦,错了好多次)
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 10010 * 2;
const int MAXR = 50010 * 2;
int n,m,r,par[MAXN],Rank[MAXR];
struct edge{
int from,to,cost;
bool operator < (const edge& rhs)const{
return this -> cost > rhs.cost;
}
};
priority_queue<edge> heap;
void init(int n){
int i;
for(i = 0;i < n; ++i){
par[i] = i;
Rank[i] = 0;
}
}
int Find(int x){
if(par[x] == x)
return x;
return par[x] = Find(par[x]);
}
void unite(int x,int y){
int a = Find(x);
int b = Find(y);
if(a == b)
return;
if(Rank[a] > Rank[b]){
par[b] = a;
}
else{
par[a] = b;
if(Rank[a] == Rank[b])
Rank[b]++;
}
}
bool same(int x,int y){
return Find(x) == Find(y);
}
int main(){
int T,i;
int x,y,c;
scanf("%d",&T);
for(int p = 0;p < T; ++p){
scanf("%d%d%d",&n,&m,&r);
for(i = 0;i < r; ++i){
edge e;
scanf("%d%d%d",&e.from,&e.to,&e.cost);
e.cost = -e.cost;
heap.push(e);
}
int N = heap.size();
init(m + n);
int sum = 0;
while(!heap.empty()){
edge e = heap.top();
heap.pop();
int a = e.from;
int b = e.to + n;
if(!same(a,b)){
unite(a,b);
sum += e.cost;
}
}
printf("%d\n",(m + n) * 10000 + sum);
}
return 0;
}
方法二:Kruskal.