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
wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
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).
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
42
-1HintIn the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN = 10010;
const int MAXM = 100100;
const int INF = 1<<30;
struct EDG{
int to,next,cap,flow;
int cost; //每条边的单位价格
}edg[MAXM];
int head[MAXN],eid;
int pre[MAXN], cost[MAXN] ; //点0~(n-1) void init(){
eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,int cap,int cst){
edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst;
edg[eid].cap=cap; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;
edg[eid].cap=0; edg[eid].flow=0; head[v]=eid++;
} bool inq[MAXN];
bool spfa(int sNode,int eNode,int n){
queue<int>q;
for(int i=0; i<n; i++){
inq[i]=false; cost[i]= INF;
}
cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1;
q.push(sNode);
while(!q.empty()){
int u=q.front(); q.pop();
inq[u]=0;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(edg[i].cap-edg[i].flow>0 && cost[v]>cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费
cost[v] = cost[u]+edg[i].cost;
pre[v]=i; //记录路径上的边
if(!inq[v])
q.push(v),inq[v]=1;
}
}
}
return cost[eNode]!=INF; //推断有没有增广路
}
//反回的是最大流,最小花费为minCost
int minCost_maxFlow(int sNode,int eNode ,int& minCost,int n){
int ans=0;
while(spfa(sNode,eNode,n)){
ans++;
for(int i=pre[eNode]; i!=-1; i=pre[edg[i^1].to]){
edg[i].flow+=1; edg[i^1].flow-=1;
minCost+=edg[i].cost;
}
}
return ans;
}
void scanf(int &ans){
char ch;
while(ch=getchar()){
if(ch>='0'&&ch<='9')
break;
}
ans=ch-'0';
while(ch=getchar()){
if(ch<'0'||ch>'9')
break;
ans=ans*10+ch-'0';
}
}
int mapt[1005][1005];
int main(){
int n,m , u, v, d ;
while(scanf("%d%d",&n,&m)>0){
init();
int s=0, t=2*n+1; for(int i=1; i<=n; i++){
addEdg(s , i , 1 , 0);
addEdg(i+n , t , 1 , 0);
for(int j=1; j<=n; j++)
mapt[i][j]=INF;
}
while(m--){
scanf(u); scanf(v); scanf(d);
if(mapt[u][v]>d)
mapt[u][v]=d;
}
for( u=1; u<=n; u++)
for(v=1; v<=n; v++)
if(mapt[u][v]!=INF)
addEdg(u,v+n,1,mapt[u][v]); int mincost=0;
n-= minCost_maxFlow(s , t , mincost , t+1);
if(n==0)
printf("%d\n",mincost);
else
printf("-1\n");
}
}
HDU 1853 Cyclic Tour(最小费用最大流)的更多相关文章
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
- 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 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
随机推荐
- 17 C#中的循环执行 while循环
在编程中有代码的执行主要有三种方式.(1)顺序执行,也就是一条语句一条语句按顺序执行:(2)条件执行,也就是if...else.当某种条件满足时执行一些代码:(3)循环执行,就是当某种条件满足的时候, ...
- Eclipse+JUnit+Selenium配置
运行环境:Windows XP.Firefox.Firefox需要安装在标准路径下"C:\Program Files\Mozilla Firefox\firefox.exe",否则 ...
- oracle添加联合主键
1 alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为 ...
- QT,折腾的几天-----关于 QWebEngine的使用
几天前,不,应该是更早以前,就在寻找一种以HTML5+CSS+Javascript的方式来写桌面应用的解决方案,为什么呢?因为前端那套可以随心所欲的写样式界面啊,恩.其实我只是想使用H5的一些新增功能 ...
- ThinkPHP---thinkphp完善站内信功能
[一]收件箱 分析 控制器:EmailController.class.php 方法:recBox(全称receive box收件箱) 模板文件:recBox.html 分步操作: 第一步:创建方法r ...
- @EnableConfigurationProperties
参考:https://www.jianshu.com/p/7f54da1cb2eb 使用 @ConfigurationProperties 注解的类生效. 如果一个配置类只配置@Configurati ...
- js 动态加载select触发事件
动态加载select后,手动调用一下 subjectChange函数,模拟触发change事件 function hallidChange(value) { $.ajax({ type: " ...
- TWaver推智能手表挑战华为苹果
2015年的春节刚过,苹果.华为.三星就紧锣密鼓的发布了各自新产品.华为.苹果的智能手表最吸引眼球.TWaver也不甘示弱,立刻连夜推出了更像传统奢侈豪华手表的TWaver Watch,予以反击.看来 ...
- struts2源码下载链接
http://blog.csdn.net/qq_qun_247286682/article/details/6975298
- Xcode编译ffmpeg(2)
iOS: FFmpeg编译和使用问题总结 折磨了我近一周多时间的FFmpeg库编译问题终于解决了,必须得把这一段时间来遇到过的坑全写出来.如果急着解决问题,编译最新版本的FFmpeg库请直接看第二部分 ...