您的位置:首页 > 其它

hdu 3488 Tour(费用流,去重边)

2016-08-02 16:27 369 查看


Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2926    Accepted Submission(s): 1408


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

题意:让图中的所有点都划分到一个环内,问最小的代价是多少

思路:把每个点都拆分成两个点,有路径的连一条边,设置超级源点和超级汇点,跑一遍MCMF即可

注意去重边,不然会TLE

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define M 30010
#define INF 0x3f3f3f3f
struct Node
{
int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用
} edge[M*7];
int cnt,head
;
int vis
,d
,pp
;
int flag[250][250];
int sumflow;///最大流量总和
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].cost=cost;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].from=to;
edge[cnt].to=from;
edge[cnt].cost=-cost;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;///存反向边
}
int spfa(int s,int t,int n)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
for(int i=0; i<=n; i++)
d[i]=INF;
d[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost)
{
d[v]=d[u]+edge[i].cost;
pp[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(d[t]==INF) return 0;///找不到一条到终点的路
return 1;
}
int MCMF(int s,int t,int n)
{
int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量
while(spfa(s,t,n))///找当前的最短路
{
minflow=INF+1;
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
flow+=minflow;///总流量加上最小流量
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
{
edge[i].cap-=minflow;///当前边减去最小流量
edge[i^1].cap+=minflow;///反向边加上最小流量
}
mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
}
sumflow=flow;
return mincost;
}
int main()
{
int n,m,T,t=1;
int from,to,cost;
scanf("%d",&T);
while(T--)
{
memset(flag,-1,sizeof(flag));
scanf("%d %d",&n,&m);
init();
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&from,&to,&cost);
if(flag[from][to]==-1||cost<flag[from][to])
flag[from][to]=cost;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(flag[i][j]!=-1)
addedge(i,j+n,1,flag[i][j]);

int S=0,T=2*n+1;
for(int i=1; i<=n; i++)
addedge(S,i,1,0);
for(int i=n+1;i<=2*n;i++)
addedge(i,T,1,0);
int ans=MCMF(S,T,T);///流量为2保证只会走两次,如果是1到n有可能走多次
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: