Cyclic Tour HDUOJ 费用流

Cyclic Tour


Time
Limit: 1000/1000 MS (Java/Others)    Memory
Limit: 32768/65535 K (Java/Others)
Total Submission(s):
1399    Accepted Submission(s):
712

Problem Description

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

Output one number for each test case,
indicating the minimum length of all the tours. If there are no such
tours, output -1.

Sample Input

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 6 5 1 2 1 2 3 1 3 4
1 4 5 1 5 6 1

Sample Output

42-1

Hint

In the first sample, there are two cycles, (1->2->3->1) and
(6->5->4->6) whose length is 20 + 22 = 42.

Author

[email protected]

Source

HDU 2007 Programming Contest - Final

Recommend

lcy   |   We
have carefully selected several similar problems for
you:  1533 3395 3315 1565 2448

费用流,拆点,source连接每个入点,费用0,容量1,每个出点连sink,费用0,容量1,入点出点见连费用边权,容量1,裸的费用流。

只要注意一下spfa的队列数组开大一点。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 210
#define INF 0x3f3f3f3f
//AC
int n,m;
struct Edge
{
int np,val,c;
Edge *next,*neg;
}E[MAXN*MAXN],*V[MAXN*2];
int tope,sour=0,sink=1;
void add_edge(int x,int y,int z,int c)
{
//cout<<"Add"<<x<<" "<<y<<" "<<z<<" "<<c<<endl;
E[++tope].np=y;
E[tope].val=z;
E[tope].c=c;
E[tope].next=V[x];
V[x]=&E[tope];
E[++tope].np=x;
E[tope].val=0;
E[tope].c=-c;
E[tope].next=V[y];
V[y]=&E[tope];

E[tope].neg=&E[tope-1];
E[tope-1].neg=&E[tope];
}
int q[MAXN*10],vis[MAXN],dis[MAXN],dfn=0;
int prev[MAXN];
Edge *path[MAXN];
int spfa()
{
int ope=-1,clo=0,now;
Edge *ne;
memset(dis,INF,sizeof(dis));
dfn++;
q[0]=sour;
vis[sour]=dfn;
dis[sour]=0;
while (ope<clo)
{
now=q[++ope];
vis[now]=0;
for (ne=V[now];ne;ne=ne->next)
{
if (ne->val&&dis[ne->np]>dis[now]+ne->c)
{
dis[ne->np]=dis[now]+ne->c;
prev[ne->np]=now;
path[ne->np]=ne;
if (vis[ne->np]!=dfn)
{
vis[ne->np]=dfn;
q[++clo]=ne->np;
}
}
}
}
return dis[sink];
}
pair<int,int> max_cost_flow()
{
int ds,fl,now,x;
pair<int,int> ret;
ret.first=ret.second=0;
while (ds=spfa(),ds!=INF)
{
x=sink;
fl=INF;
while (x!=sour)
{
fl=min(fl,path[x]->val);
x=prev[x];
}
x=sink;
while (x!=sour)
{
path[x]->val-=fl;
path[x]->neg->val+=fl;
x=prev[x];
}
ret.first+=fl;
ret.second+=ds*fl;
}
return ret;
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,x,y,z;
while (~scanf("%d%d",&n,&m))
{
tope=-1;
memset(V,0,sizeof(V));
for (i=0;i<n;i++)
{
add_edge(sour,2+i,1,0);
add_edge(2+i+n,sink,1,0);
}
for(i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);x--;y--;
add_edge(2+x,2+y+n,1,z);
}
pair<int,int> p1;
p1=max_cost_flow();
if (p1.first!=n)
{
printf("-1\n");
}else
{
printf("%d\n",p1.second);
}
}
}

Cyclic Tour HDUOJ 费用流

时间: 2024-10-10 15:02:59

Cyclic Tour HDUOJ 费用流的相关文章

POJ2135_Farm Tour(网络流/费用流)

解题报告 题目传送门 题意: 一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路. 思路: 如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了. 一次mcmf模版. #include <iostream> #include <cstring> #include <queue> #include <cstdio> #define inf 0x3f3f3f3f using namespace st

hdu 1853 Cyclic Tour 最小费用最大流

题意:一个有向图,现在问将图中的每一个点都划分到一个环中的最少代价(边权和). 思路:拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. /********************************************************* file name: hdu1853.cpp author : kereo create time: 2015年02月16日 星期一 17时38分51秒 *******

[网络流]Farm Tour(费用流

Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

费用流 hdu1853 Cyclic Tour

传送门:点击打开链接 题意:给n个点和M条有向边,要找出许多个环出来,每个环点至少有2个,所有的点都要被环覆盖1次,且只能为1次.问所有环的长度之和 这题也可以用KM来做,这里主要是练习费用流的建图 对于这题,建图也是非常的奇妙的 由于每个点的入度都是1,出度都是1 所以会想到把每个点拆分成2个点,用i和i+n来表示 然后将源点与所有的i连接起来,将汇点与所有的i+n连接起来,容量都是1 对于每一条边(u,v),添加边(u,v+n,1,cost),让第一层的点连接到第二层去 其实就是将点拆分成两

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

POJ 2677 Tour 双调旅行商 dp, double+费用流

题目链接:点击打开链接 题意:给定二维平面上的n个点 从最左端点到最右端点(只能向右移动) 再返回到到最右端点(只能向左移动,且走过的点不能再走) 问最短路. 费用流: 为了达到遍历每个点的效果 把i点拆成 i && i+n 在i ->i+n 建一条费用为 -inf 的边,流量为1 这样跑最短路时必然会经过这条边,以此达到遍历的效果. dp :点击打开链接 对于i点 :只能跟一个点相连 -- 1.跟 i-1点相连 2.不跟i-1相连 用dp[i][j] 表示两个线头为 i 和 j 的

POJ 2135 Farm Tour [最小费用最大流]

题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很难,因为要保证每条边只能走一次,那么我们把边拆为两个点,一个起点和终点,容量是1,权重是这条路的长度.然后两个端点分别向起点连接容量是1权重是0的边,终点分别向两个端点连容量是1权重是0的边,从源点到1连容量为2权重为0的边,从n到汇点连容量为2权重为0的边. #include<stdio.h>