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
RoBa@TJU
 
Source
 
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*];
int tope,sour=,sink=;
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=;
E[tope].c=-c;
E[tope].next=V[y];
V[y]=&E[tope]; E[tope].neg=&E[tope-];
E[tope-].neg=&E[tope];
}
int q[MAXN*],vis[MAXN],dis[MAXN],dfn=;
int prev[MAXN];
Edge *path[MAXN];
int spfa()
{
int ope=-,clo=,now;
Edge *ne;
memset(dis,INF,sizeof(dis));
dfn++;
q[]=sour;
vis[sour]=dfn;
dis[sour]=;
while (ope<clo)
{
now=q[++ope];
vis[now]=;
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=;
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=-;
memset(V,,sizeof(V));
for (i=;i<n;i++)
{
add_edge(sour,+i,,);
add_edge(+i+n,sink,,);
}
for(i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);x--;y--;
add_edge(+x,+y+n,,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 费用流的更多相关文章

  1. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  2. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  3. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  4. [网络流]Farm Tour(费用流

    Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...

  5. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

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

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

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

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  8. Tour HDU - 3488 有向环最小权值覆盖 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...

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

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

随机推荐

  1. Hrbust1328 相等的最小公倍数 (筛素数,素因子分解)

    本文出自:http://blog.csdn.net/svitter/ 题意: 求解An 与 An-1是否相等. n分为两个情况-- 1.n为素数, 2.n为合数. =  =好像说了个废话..素数的时候 ...

  2. sift算法c语言实现

    前段时间在做三维測量方面的研究.须要得到物体表面三维数据.sift算法是立体匹配中的经典算法.以下是对RobHess的SIFT源码的凝视.部分内容參考网上,在这里向各位大神表示感谢. http://b ...

  3. Android开发全套视频教程在线观看网盘下载

    千锋金牌讲师老罗老师简介: 国内第一批Android教学讲师,10多年软件开发经验,6年多教学经验,曾担任广东电信北京分公司移动事业部项目经理,主持过微软中国平台考试系统.山西省旅游局智能化平台等大型 ...

  4. CSS背景颜色、背景图片、平铺、定位、固定

    CSS背景颜色设置 background-color:red;如设置背景颜色为红色: 背景颜色设置支持3种写法: 颜色名 16进制 rgb CSS背景图片颜色设置 background-image:u ...

  5. GitHub详细教程

    GitHub详细教程 Table of Contents 1 Git详细教程 1.1 Git简介 1.1.1 Git是何方神圣? 1.1.2 重要的术语 1.1.3 索引 1.2 Git安装 1.3 ...

  6. Redhat linux DNS配置指南(SCANIP配置手册)

    在oracle 11g的RAC中增加了SCAN IP,而使用 SCAN IP的一种方式就是使用DNS,这里介绍在Redhat Linux 5.4中DNS的详细配置操作在配置DNS之前修改主机名Redh ...

  7. 《转》15种CSS混合模式让图片产生令人惊艳的效果

    浏览器支持 按照现在情况来讲, 浏览器支持 CSSbackground-blend-mode属性还在不断的完善中.早期版本的浏览器目前还不支持,但caniuse.com报告说在Chrome,Firef ...

  8. 新建线程与UI线程间的通信

    现在用一个实例来演示一下自己的新建线程与UI线程间的通信. UI界面包含3个控件: 一个输入框,用来输入数字: 一个显示框,用来显示从2开始,到输入数字之间的所有质数: 一个按钮,点击后获取输入框输入 ...

  9. Android学习2--项目文件列表简单分析

    使用Eclipse创建的默认项目文件列表如下: src:src目录是Android工程的源程序目录,该目录用于存放Java项目的源代码 gen:gen目录存放所有自动生成的文件,在这个目录中最关键的文 ...

  10. Java单例模式深入详解

    原文地址:http://www.cnblogs.com/hxsyl/ 仅作为笔记收藏…… 一.问题引入 偶然想想到的如果把Java的构造方法弄成private,那里面的成员属性是不是只有通过stati ...