这个类比最小覆盖去做匹配,对一个点,他一定只在一个环上,那么他必定只有一个入度,一个出度,这样就可以把出度和入度两两匹配,然后找匹配价值的最小值就好了。。

然后一个坑了窝不久的是自己的km板子是得建完全图才能跑得懂的,所以对于一些无用边也得给个inf的权值加进图中。。

而判断能不能用有向环覆盖的条件是没有用到那些inf的边,即最终答案要小于inf。。

 

 

 

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 205
#define nm 60005
#define pi 3.1415926535897931
const int inf=2000000+5;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}





struct edge{int t,v;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y,int v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
int n,m,ans,lx[NM],ly[NM],_x,_y,_t,match[NM],b[NM],pre[NM],a[NM][NM];
bool v[NM];




bool bfs(int k){
int x,y=0,t=0,s;
mem(pre);mem(v);
inc(i,1,n)b[i]=inf;
match[y]=k;
do{
x=match[y];s=inf;v[y]=true;
link(x)if(!v[j->t]){
if(b[j->t]>lx[x]+ly[j->t]-j->v)b[j->t]=lx[x]+ly[j->t]-j->v,pre[j->t]=y;
if(s>b[j->t])s=b[j->t],t=j->t;
}
inc(i,0,n)if(v[i])lx[match[i]]-=s,ly[i]+=s;else b[i]-=s;
y=t;
}while(match[y]);
for(;y;y=pre[y])match[y]=match[pre[y]];
return true;
}


int main(){
int _=read();while(_--){
n=read();m=read();
ans=0;mem(e);mem(h);o=e;mem(lx);mem(ly);mem(match);
inc(i,1,n)inc(j,1,n)a[i][j]=inf;
inc(i,1,m){_x=read();_y=read();_t=read();a[_x][_y]=min(a[_x][_y],_t);}
inc(i,1,n)inc(j,1,n)add(i,j,-a[i][j]);
bool f=false;
inc(i,1,n)if(!bfs(i)){f=true;break;}
if(f){printf("-1\n");continue;}
inc(i,1,n)if(match[i])ans+=ly[i]+lx[match[i]];
if(-ans<inf)printf("%d\n",-ans);else printf("-1\n");
}
return 0;
}

 

 

 

 

 

Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4224    Accepted Submission(s): 2010


 

Problem Description

In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.

 

 

Input

An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.

 

 

Output

For each test case, output a line with exactly one integer, which is the minimum total distance.

 

 

Sample Input

1 6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4

 

 

Sample Output

42

 

 

Source

​2010 ACM-ICPC Multi-University Training Contest(6)——Host by BIT ​

 

 

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  ​​1533​​​ ​​3435​​​ ​​3395​​​ ​​3491​​​ ​​3657​​ 

 

 

​Statistic​​​ | ​​Submit​​​ | ​​Discuss​​​ | ​​Note​