【刷题】HDU 1853 Cyclic Tour
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.
Description(CHN)
给你一个 \(N\) 个点 \(M\) 条边的带权有向图,现在要你求这样一个值:
该有向图中的所有顶点正好被1个或多个不相交的有向环覆盖.
这个值就是 所有这些有向环的权值和. 要求该值越小越好.
Solution
有向图环覆盖,变成二分图匹配,经典套路
然后要求权最小,就是二分图最大匹配,跑遍费用流就好了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=100+10,MAXM=MAXN*MAXN,inf=0x3f3f3f3f;
int n,m,e,beg[MAXN<<1],s,t,level[MAXN<<1],nex[MAXM<<1],to[MAXM<<1],cap[MAXM<<1],was[MAXM<<1],p[MAXN<<1],cur[MAXN<<1],vis[MAXN<<1],clk,answas;
std::queue<int> q;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z,int k)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
cap[e]=z;
was[e]=k;
to[++e]=x;
nex[e]=beg[y];
beg[y]=e;
cap[e]=0;
was[e]=-k;
}
inline bool bfs()
{
for(register int i=1;i<=t;++i)level[i]=inf;
level[s]=0;
p[s]=1;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
p[x]=0;
for(register int i=beg[x];i;i=nex[i])
if(cap[i]&&level[to[i]]>level[x]+was[i])
{
level[to[i]]=level[x]+was[i];
if(!p[to[i]])p[to[i]]=1,q.push(to[i]);
}
}
return level[t]!=inf;
}
inline int dfs(int x,int maxflow)
{
if(x==t||!maxflow)return maxflow;
int res=0;
vis[x]=clk;
for(register int &i=cur[x];i;i=nex[i])
if((vis[x]^vis[to[i]])&&cap[i]&&level[to[i]]==level[x]+was[i])
{
int f=dfs(to[i],min(maxflow,cap[i]));
res+=f;
cap[i]-=f;
cap[i^1]+=f;
answas+=was[i]*f;
maxflow-=f;
if(!maxflow)break;
}
vis[x]=0;
return res;
}
inline int MCMF()
{
int res=0;
while(bfs())clk++,memcpy(cur,beg,sizeof(cur)),res+=dfs(s,inf);
return res;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
e=1;memset(beg,0,sizeof(beg));answas=0;
for(register int i=1;i<=m;++i)
{
int u,v,k;read(u);read(v);read(k);
insert(v,u+n,1,k);
}
s=n+n+1,t=s+1;
for(register int i=1;i<=n;++i)insert(s,i,1,0),insert(i+n,t,1,0);
if(MCMF()!=n)puts("-1");
else write(answas,'\n');
}
return 0;
}
【刷题】HDU 1853 Cyclic Tour的更多相关文章
- hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 1853 Cyclic Tour[有向环最小权值覆盖]
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 1853 Cyclic Tour(最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
- 最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #incl ...
- HDU(1853),最小权匹配,KM
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...
- hdu 1853 最小费用流好题 环的问题
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)
前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...
随机推荐
- 04-matplotlib-柱形图
import numpy as np import matplotlib.pyplot as plt # 柱形图 # 例一 N =5 y = [15,28,10,30,25] index = np.a ...
- PHP 包含文件
1.require test123.php <?php $a=1; 运行文件: <?php require('test123.php'); echo 'Hello!'; echo '< ...
- 工作小应用:EXCEL查找两列重复数据
工作案例:excel存在A列.B列,需要找出B列没有A列的数据,具体做法如下(以office2007做案例): 1.点击 公式-定义名称 ,选中A列,填写名称“AAA”,选中B列,填写名称“BBB”: ...
- lambda----jdk8重头戏
简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...
- 第十周psp作业
本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图
- Percona XtraDB Cluster 5.7
附加:相关在线文档https://www.percona.com/software/documentation 安装要求: 1.root权限2.保证开放3306.4444.4567.4568端口3.关 ...
- 《Spring2之站立会议2》
<Spring2之站立会议2> 昨天,模仿着资料把客户端和服务器端的代码写了一下: 今天,继续找本机的端口号和逐步深入理解代码含义: 遇到的问题,在理解时,对一些知识理解还是比较朦胧,一知 ...
- 作业45//Calculator::3.0
计算器 github 我的天我到底要写什么 一,2.0及2.6的改动 做了计算部分 加入了判断输入是否合法 合法的定义是算式符合`数字+运算符+数字+运算符+数字`的格式 其中`"-&quo ...
- Alpha 冲刺报告(4/10)
Alpha 冲刺报告(4/10) 队名:洛基小队 峻雄(组长) 已完成:继续行动脚本的编写 明日计划:尽量完成角色的移动 剩余任务:物品背包交互代码 困难:具体编码进展比较缓慢 ----------- ...
- 团队作业4Alpha冲刺(真.三英战吕布团队)
第一天 2018/6/13 1.1 今日完成任务情况以及遇到的问题. 1.1.1:完成前台部分界面优化,后台进行代码优化 1.1.2团队前台部分js.jquery部分功能实现有难度. 1.2 明天任务 ...