地址:点击打开链接
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.
InputAn integer T in the first line indicates the number of the test cases.
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.
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.OutputFor 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 4Sample Output
42
题意:
给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只被一个有向环覆盖.问你该有向环所有权值的总和最小是多少?(保证有解)
思路:
第一次接触这种题目,参考:(点击打开链接),此类有向环覆盖问题,建图方式基本一样,其中也用到了拆点,比较重要,现,容量为1,代表只允许通过一次,建图如下:
(1)源点s编号0, 图中原始节点拆点成i和i+n(编号1到n和编号n+1到2*n) , 汇点t编号2*n+1.
(2)如果原图i节点到j节点有权值w的边,那么新图有边 (i, j+n, 1, w)
(3)且源点s到任意节点i有边 (s, i, 1, 0)
(4)且任意节点i到汇点t有边 (i+n,t,1,0)
如果最大流==n,那么最小费用即是解.(最重要一点)
代码:
#include<map>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define maxn 400+5
#define PI acos(-1.0)
#define INF 1e9
using namespace std;
typedef long long ll;
struct Edge{
int from,to,cap,flow,cost;
Edge(){}
Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};
struct MCMF{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init(int n,int s,int t){
this->n=n;
this->s=s;
this->t=t;
for(int i=0;i<n;i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost){
edges.push_back((Edge){from,to,cap,0,cost});
edges.push_back((Edge){to,from,0,0,-cost}) ;
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int &flow,int &cost){
for(int i=0;i<n;i++) d[i]=INF;
memset(inq,0,sizeof(inq));
d[s]=0; inq[s]=1; p[s]=0; a[s]=INF;
queue<int>Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
inq[u] =0;
for(int i=0;i<(int)G[u].size();i++){
Edge& e=edges[G[u][i]];
if(e.cap>e.flow &&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]) {
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==INF) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s) {
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
int Mincost(int nn){
int flow=0;
int cost=0;
while(BellmanFord(flow,cost));
return flow==nn?cost:-1;
}
}MM;
int main()
{
int t;
int n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int src=0,dst=2*n+1;
MM.init(2*n+2,src,dst);
for(int i=1;i<=n;i++){
MM.AddEdge(src,i,1,0);
MM.AddEdge(i+n,dst,1,0);
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
MM.AddEdge(u,v+n,1,w);
}
printf("%d\n",MM.Mincost(n));
}
return 0;
}