Cyclic Tour

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

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 
 

题意:
  给你一个 N 个顶点 M 条边的带权有向图, 要你把该图分成 1 个或多个不相交的有向环. 且所有点都只被一个有向环覆盖.

  问你该有向环所有权值的总和最小是多少?(保证有解)

解析:

  任意类似的【有向环最小权值覆盖】问题,都可以用最小费用流来写。
  由于题目中要求每个点最多走一次,为了防止走多次的发生,我们要把每个点 i 拆成左部点i和右部点i+n两个点。

具体建图如下:

  1、S向各点连<1,0>(前者表示容量,后者表示花费)
  2、各点向T连<1,0>
  3、如果i与j之间有连边,i向j+n连<1,w[i,j]>
最终如果最大流 == n 的话(即满流),那么最小费用就是我们所求,否则输出-1;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=;
const int M=2e5+;
const int inf=0x3f3f3f3f;
struct edge{int v,cap,cost,next;}e[M<<];int tot=,head[N];
int n,m,cas,ans,res,S,T,dis[N],Prev[N],flow[N],q[N*];
bool vis[N];
void add(int x,int y,int z,int cost){
e[++tot].v=y;e[tot].cap=z;e[tot].cost=cost;e[tot].next=head[x];head[x]=tot;
e[++tot].v=x;e[tot].cap=;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
}
bool spfa(){
for(int i=S;i<=T;i++) vis[i]=,dis[i]=inf;
int h=,t=;q[t]=S;dis[S]=;flow[S]=inf;
while(h!=t){
int x=q[++h];vis[x]=;
for(int i=head[x];i;i=e[i].next){
if(e[i].cap&&dis[e[i].v]>dis[x]+e[i].cost){
dis[e[i].v]=dis[x]+e[i].cost;
Prev[e[i].v]=i;
flow[e[i].v]=min(flow[x],e[i].cap);
if(!vis[e[i].v]){
vis[e[i].v]=;
if(dis[e[i].v]<dis[x])
q[h--]=e[i].v;
else
q[++t]=e[i].v;
}
}
}
}
return dis[T]!=inf;
}
void augment(){
for(int i=T;i!=S;i=e[Prev[i]^].v){
e[Prev[i]].cap-=flow[T];
e[Prev[i]^].cap+=flow[T];
}
res+=flow[T];
ans+=dis[T]*flow[T];
}
void init(){
res=ans=;tot=;
memset(head,,sizeof head);
}
int main(){
while(scanf("%d%d",&n,&m)==){
init();
S=,T=n<<|;
for(int i=;i<=n;i++) add(S,i,,),add(i+n,T,,);
for(int i=,x,y,w;i<=m;i++) x=read(),y=read(),w=read(),add(x,y+n,,w);;
while(spfa()) augment();
if(res==n) printf("%d\n",ans);
else printf("-1\n");
}
return ;
}

HDU 1853 Cyclic Tour[有向环最小权值覆盖]的更多相关文章

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

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

  2. HDU 3488 Tour(最小费用流:有向环最小权值覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...

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

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

  4. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

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

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

  6. 【刷题】HDU 1853 Cyclic Tour

    Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...

  7. ZOJ-2342 Roads 二分图最小权值覆盖

    题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...

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

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

  9. 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour

    题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...

随机推荐

  1. 站点部署,IIS配置优化指南

    目录 一. 二. 三. 四. 五. 六. 七.       安全性 八.       多服务器IIS集中化管理web 通常把站点发布到IIS上运行正常后,很少会去考虑IIS提供的各种参数,如何配置才是 ...

  2. Codeforces 323C Two permutations

    题目描述 You are given two permutations pp and qq , consisting of nn elements, and mm queries of the for ...

  3. 某考试 T2 Tree

    2 树 2.1 题目描述 给一棵n 个节点的树,节点分别编号为0 到n - 1.你可以通过如下的操作来修改这棵树:首先先删去树上的一条边,此时树会分裂为两个连通块,然后在两个连通块之间加上一条新的边使 ...

  4. 【spring boot hibernate】hibernate命名策略spring.jpa.hibernate.naming-strategy不起作用

    对于 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy hibernate命名策略设置之后 ...

  5. Facebook Rebound 弹性动画库 源码分析

    Rebound源码分析 让动画不再僵硬:Facebook Rebound Android动画库介绍一文中介绍了rebound这个库. 对于想体验一下rebound的效果,又懒得clone和编译代码的, ...

  6. 解决 ultraedit 菜单字体模糊

    新装完ultraedit后,使用时菜单里的字体非常小,模糊不清非常不爽. 1.安装好UltraEdit后,到安装目录里,复制一份Uedit32.exe文件用于修改.2.使用UltraEdit打开复制的 ...

  7. hdu1070(C++)

    本题在于求单价,即为每一天(每200升牛奶)要多少钱,注意超过1000的当做5天,不足200的忽略 #include<iostream> #include<string> us ...

  8. 关闭火狐定期向“http://detectportal.firefox.com/”发包

    问题:最近发现火狐总是向http://detectportal.firefox.com/发包 办法:firefox地址栏输入 about:config,然后搜索找到 network.captive-p ...

  9. context:exclude-filter spring事宜【经典-转】

    context:exclude-filter spring事务 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行. ...

  10. ios 开发keywordIBInspectable

    这个keyword 能够让开发人员省去非常多事情 把部分界面设置 放在ui设计这里 能够让别人分担 特别是 像我如今所呆的小公司老变样 让他们也能够改 这里仅仅点出一下 有兴趣 自行搜索