There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
题意:有n个城市和m条路,Tom要做几次环游,要求每个环至少包含2个城市且每个城市只能属于一个环,计算整个旅行所走的最短路程
思路:这种求圈的最小权值,用最小费用最大流是一种思路(一种通用方法0.0),我们可以先建立一个源点和汇点s,t ,然后把每个点拆成两个点分别为0-n-1和n-2n-1,
然后连接s和0-n-1的点,连接n-2n-1的点和t,容量都为1,费用都为0。最后,i和j有连线的话就 add ( i , n+j , 1 , cost ),如果能够都形成圈的话,那么最大流就等于城市的个数(因为容量都为1,所以保证每个城市都只经过一次),若不等于那就输出-1,能答案就是最小费用
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long LL;
using namespace std;
const int maxn = 4e5 + 5;
int n,m,s,t;
int head[300],cnt;
int vis[300], d[300], q[100000], cur[300];
struct node
{
int u, v, cap, cost, next;
}edge[100000];
void init(){
memset(head,-1,sizeof(head));
cnt=0;
}
void addedge(int u, int v, int cap, int cost)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].cost=-cost;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int spfa(int &flow,int &cost)
{
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
d[s]=0;
cur[s]=-1;
int f1=0 ,f2=0, i, minflow=INF;
q[f1++]=s;
while(f1>=f2)
{
int u=q[f2++];
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
{
d[v]=d[u]+edge[i].cost;
if(minflow>edge[i].cap)
{
minflow=edge[i].cap;
}
cur[v]=i;
if(!vis[v])
{
q[f1++]=v;
vis[v]=1;
}
}
}
}
if(d[t]==INF) return 0;
flow+=minflow;
cost+=minflow*d[t];
for(i=cur[t];i!=-1;i=cur[edge[i^1].v])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
return 1;
}
int solve(){
int flow=0,cost=0;
while (spfa(flow,cost));//flow最大流,cost最小费用
return flow==n?cost:-1;
}
int main ()
{
//freopen ("in.txt","r",stdin);
while (~scanf ("%d%d",&n,&m)){
init();
s=2*n; t=s+1;
for (int i=0;i<n;i++) {
addedge(s,i,1,0);
addedge(n+i,t,1,0);
}
int u,v,cost;
for (int i=0;i<m;i++){
scanf ("%d%d%d",&u,&v,&cost);
addedge(u-1,v+n-1,1,cost);
}
printf ("%d\n",solve());
}
return 0;
}