裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流=2时,有solution,此时费用即answer.

--------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
 
#define rep(i,n) for(int i=0;i<n;++i)
#define clr(x,c) memset(x,c,sizeof(x))
 
using namespace std;
 
const int maxn=100+5;
const int inf=1e10;
 
struct Edge {
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):
from(u),to(v),cap(c),flow(f),cost(w) {}
};
 
struct MCMF {
int n,m,s,t,MF;
bool inq[maxn];
int d[maxn];
int a[maxn];
int p[maxn];
vector<int> g[maxn];
vector<Edge> edges;
void init(int n) {
this->n=n;
rep(i,n) g[i].clear();
edges.clear();
}
void addEdge(int from,int to,int cap,int cost) {
edges.push_back( Edge(from,to,cap,0,cost) );
edges.push_back( Edge(to,from,0,0,-cost) );
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
bool spfa(int s,int t,int &flow,int &cost) {
rep(i,n) d[i]=inf;
clr(inq,0);
d[s]=0; inq[s]=1; p[s]=0; a[s]=inf;
queue<int> q;
q.push(s);
while(!q.empty()) {
int x=q.front(); q.pop();
inq[x]=0;
rep(i,g[x].size()) {
Edge &e=edges[g[x][i]];
if(e.cap>e.flow && d[e.to]>d[x]+e.cost) {
d[e.to]=d[x]+e.cost;
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
if(!inq[e.to]) { q.push(e.to); inq[e.to]=1; }
}
}
}
if(d[t]==inf) return false;
flow+=a[t];
cost+=d[t]*a[t];
int x=t;
while(x!=s) {
edges[p[x]].flow+=a[t];
edges[p[x]^1].flow-=a[t];
x=edges[p[x]].from;
}
return true;
}
int min_cost(int s,int t) {
int cost=0;
MF=0;
while(spfa(s,t,MF,cost));
return cost;
}
} mcmf;
 
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
int n;
while(scanf("%d",&n)==1 && n) {
mcmf.init(n+2);
int m;
int a,b,c;
scanf("%d",&m);
while(m--) {
scanf("%d%d%d",&a,&b,&c);
mcmf.addEdge(a,b,1,c);
mcmf.addEdge(b,a,1,c);
}
mcmf.addEdge(0,1,2,0);
mcmf.addEdge(n,n+1,2,0);
int cost=mcmf.min_cost(0,n+1);
mcmf.MF==2 ? printf("%d\n",cost) : printf("Back to jail\n");
}
return 0;
}

--------------------------------------------------------------------------------

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Problem ?
Dijkstra, Dijkstra.
Time Limit: 10 seconds

Dexter: "You don't understand. I can't walk...
they've tied my shoelaces together."

Topper Harley: "A knot. Bastards!"

Jim Abrahams and Pat Proft,
"Hot Shots! Part Deux."

You are a political prisoner in jail. Things are looking grim, but fortunately, your jailmate has come up with an escape plan. He has found a way for both of you to get out of the cell and run through the city to the train station, where you will leave the country. Your friend will escape first and run along the streets of the city to the train station. He will then call you from there on your cellphone (which somebody smuggled in to you inside a cake), and you will start to run to the same train station. When you meet your friend there, you will both board a train and be on your way to freedom.

Your friend will be running along the streets during the day, wearing his jail clothes, so people will notice. This is why you can not follow any of the same streets that your friend follows - the authorities may be waiting for you there. You have to pick a completely different path (although you may run across the same intersections as your friend).

What is the earliest time at which you and your friend can board a train?

Problem, in short
Given a weighed, undirected graph, find the shortest path from S to T and back without using the same edge twice.

Input
The input will contain several test cases. Each test case will begin with an integer n (2<=n<=100) - the number of nodes (intersections). The jail is at node number 1, and the train station is at node number n. The next line will contain an integer m - the number of streets. The next m lines will describe the m streets. Each line will contain 3 integers - the two nodes connected by the street and the time it takes to run the length of the street (in seconds). No street will be longer than 1000 or shorter than 1. Each street will connect two different nodes. No pair of nodes will be directly connected by more than one street. The last test case will be followed by a line containing zero.

Output
For each test case, output a single integer on a line by itself - the number of seconds you and your friend need between the time he leaves the jail cell and the time both of you board the train. (Assume that you do not need to wait for the train - they leave every second.) If there is no solution, print "Back to jail".

Sample Input Sample Output
2 1 1 2 999 3 3 1 3 10 2 1 20 3 2 50 9 12 1 2 10 1 3 10 1 4 10 2 5 10 3 5 10 4 5 10 5 7 10 6 7 10 7 8 10 6 9 10 7 9 10 8 9 10 0 
Back to jail 80 Back to jail

UVa 10806 Dijkstra,Dijkstra(最小费用最大流)的更多相关文章

  1. UVa 1658 - Admiral(最小费用最大流 + 拆点)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA - 1658 Admiral (最小费用最大流)

    最短路对应费用,路径数量对应流量.为限制点经过次数,拆点为边.跑一次流量为2的最小费用最大流. 最小费用最大流和最大流EK算法是十分相似的,只是把找增广路的部分换成了求费用的最短路. #include ...

  3. UVa 1658 海军上将(最小费用最大流)

    https://vjudge.net/problem/UVA-1658 题意: 给出一个v个点e条边的有向加权图,求1~v的两条不相交(除了起点和终点外公共点)的路径,使得权和最小. 思路:把2到v- ...

  4. uva 1658 Admiral 【 最小费用最大流 】

    拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...

  5. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  6. UVA 10806 最小费用最大流

    终于可以写这道题的题解了,昨天下午纠结我一下下午,晚上才照着人家的题解敲出来,今天上午又干坐着想了两个小时,才弄明白这个问题. 题意很简单,给出一个无向图,要求从1 到 n最短路两次,但是两次不允许经 ...

  7. UVA 1658 海军上将(拆点法+最小费用限制流)

    海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...

  8. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  9. Luogu--3381 【模板】最小费用最大流

    题目链接 3381 [模板]最小费用最大流 手写堆版本 dijkstra   400+ms 看来优先队列的常数好大 #include<bits/stdc++.h> using namesp ...

随机推荐

  1. 看大数据时代下的IT架构(1)业界消息队列对比

    一.MQ(Message Queue) 即 消息队列,一般用于应用系统解耦.消息异步分发,能够提高系统吞吐量.MQ的产品有很多,有开源的,也有闭源,比如ZeroMQ.RabbitMQ. ActiveM ...

  2. C++多线程编程(三)线程间通信

    多线程编程之三——线程间通讯 作者:韩耀旭 原文地址:http://www.vckbase.com/document/viewdoc/?id=1707 七.线程间通讯 一般而言,应用程序中的一个次要线 ...

  3. 使用hibernate 分表做增删改查

    公司项目有一张表的数据量特别大.而且时间越长累积的数据量就越大. 后来DBA决定分表来解决性能问题. 分表是指   一个母体表  一群子表(结构和字段与母体表完全一样) 我们程序对母表操作其实就是对子 ...

  4. 04737_C++程序设计_第7章_类模板与向量

    例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...

  5. rownum(转载)

    对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...

  6. spring mvc 简单搭建

    文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: </load-on-startup>     </servlet>    ...

  7. liunx操作数据库

    liunx操作数据库 1.连接数据库 #mysql -uroot -p mysql -uroot -p 然后输入密码 2.关闭防火墙 #iptables -stop 开启#iptables -star ...

  8. BZOJ 3870: Our happy ending( 状压dp )

    dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)...好像有点大, 但是可以AC.... ...

  9. Redis是什么

    Redis是什么 Redis是什么,首先Redis官网上是这么说的:A persistent key-value database with built-in net interface writte ...

  10. jquery 小插件,完成“输入字段预期值的提示信息”,防html5 placeholder属性

    前言:在很多时候,我们需要文本框中显示默认值,获取焦点时,文字框中就会清空给的值,当失去焦点时,如果没有值,继续显示默认的文字,如果有输入值,就显示输入的值.现在项目中需要用到这个地方的功能比较多,于 ...