https://vjudge.net/problem/POJ-3723
n个女的,m个男的,选择一个战士需要花费10000,如果有亲密关系,需要花费10000-d;
花费最小
用了cin加了优化超时了,后来改了scanf之后答案错误
后来把e等从0开始(下标) 过了
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1e4 + 10; struct node{ int from,to,w; }e[maxn * 5]; int t,n,m,r; bool cmp(node a,node b){ return a.w > b.w; } int fa[maxn * 2]; int find(int x){ if(x == fa[x]) return x; return fa[x] = find(fa[x]); } int dijkstra(){ sort(e,e + r,cmp); int ans = 0; for(int i = 0; i < r; i++){ int t1 = find(fa[e[i].from]); int t2 = find(fa[e[i].to]); if(t1 != t2){ ans += e[i].w; fa[t2] = t1; } } return ans; } int main(){ //freopen("in","r",stdin); scanf("%d",&t); while(t--){ scanf("%d%d%d",&n,&m,&r); for(int i = 0; i < r; i++){ scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w); e[i].to += n; } for(int i = 0; i < n + m; i++) fa[i] = i; int g = dijkstra(); printf("%d ",(n + m) * 10000 - g); } return 0; }