题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用。

思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们能找到超级汇点和超级源点的流量为2就说明有两条路,输出最小值。

代码:

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 1000+5;
const int maxm = 10000+5;
const int MOD = 1e7;
const int INF = 1 << 25;
using namespace std;
struct Edge{
int to,next,cap,flow,cost;
}edge[maxm];
int head[maxn],tot;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N,M;
void init(){
N = maxn;
tot = 0;
memset(head,-1,sizeof(head));
}
void addEdge(int u,int v,int cap,int cost){
edge[tot].to = v;
edge[tot].cap = cap; //容量
edge[tot].flow = 0;
edge[tot].cost = cost;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].cap = 0;
edge[tot].flow = 0;
edge[tot].cost = -cost;
edge[tot].next = head[v];
head[v] = tot++;
}
bool spfa(int s,int t){
queue<int> q;
for(int i = 0;i < N;i++){
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u];i != -1;i = edge[i].next){
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost){
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
return pre[t] != -1;
} int MCMF(int s,int t,int &cost){
int flow = 0;
cost = 0;
while(spfa(s,t)){
int MIN = INF;
for(int i = pre[t];i != -1;i = pre[edge[i^1].to]){
if(MIN > edge[i].cap - edge[i].flow){
MIN = edge[i].cap - edge[i].flow;
}
}
for(int i = pre[t];i != -1; i = pre[edge[i^1]. to]){
edge[i]. flow += MIN;
edge[i^1]. flow -= MIN;
cost += edge[i]. cost * MIN;
}
flow += MIN;
}
return flow;
}
int main(){
int n,m,Case = 1;
while(scanf("%d%d",&n,&m) && n+m){
init();
addEdge(0,1,2,0);
addEdge(n,n + 1,2,0);
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
addEdge(u + 1,v + 1,1,w);
}
int cost;
int flow = MCMF(0,n + 1,cost);
if(flow == 2)
printf("Instance #%d: %d\n",Case++,cost);
else
printf("Instance #%d: Not possible\n",Case++);
}
return 0;
}
/*
2 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0 Instance #1: Not possible
Instance #2: 40
Instance #3: 73
*/

UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解的更多相关文章

  1. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

  2. UVALIVE 2927 "Shortest" pair of paths

    裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...

  3. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  4. POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...

  5. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  6. POJ 3068 "Shortest" pair of paths(费用流)

    [题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...

  7. P3381 【模板】最小费用最大流 题解

    CSDN同步 原题链接 前置知识: 从三种算法剖析网络流本质 简要题意: 给定网络图,求图的最大流,以及流量为最大流时的最小费用. 现在假设你们看了那篇网络流博客之后,所有人都会了 \(\text{E ...

  8. 连续最短路算法(Successive Shortest Path)(最小费用最大流)

    #include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...

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

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

随机推荐

  1. 网页头部的声明应该是用 lang="";

    我们经常需要用缩写的代码来表示一种语言,比如用en表示英语,用de表示德语.ISO 639就是规定语种代码的国际标准.最早的时候,ISO 639规定的代码是,用两个拉丁字母表示一种语言,这被称为ISO ...

  2. java中生成流水号的一个例子(使用BerkeleyDB)

    package com.jiaoyiping.berkeleydb; import com.sleepycat.je.*; import com.sleepycat.utilint.StringUti ...

  3. FlashDevelop导入swc库

    项目不是AS项目,而是基于FlashIDE. 一 将SWC放入项目lib文件夹下 二 选择lib文件夹,右键,选择添加到库 三  FlashIDE中选择 文件-ActionScript设置 四 选择浏 ...

  4. 【BZOJ1879】[Sdoi2009]Bill的挑战 状压DP

    [BZOJ1879][Sdoi2009]Bill的挑战 Description Input 本题包含多组数据.  第一行:一个整数T,表示数据的个数.  对于每组数据:  第一行:两个整数,N和K(含 ...

  5. WCF(五) 深入理解绑定

    适用于本机WCF-WCF交互性能最佳的绑定: 允许跨主机,但只能用于部署同一台主机上,不能访问命名管道 netNamePipeBinding总结 一 WCF与SOA SOA是一种通过为所有软件提供服务 ...

  6. ExtJS学习

    ExtJS是一门比较纠结的框架,自己不太熟,因为现在在做一些老项目,所以没办法要学点.记录下.其实Ext也不是很难,主要是多查查API,了解其基本的用法,然后慢慢去学习,学成之后做管理系统还是很有优势 ...

  7. thinkphp---手机访问切换模板!

    手机访问切换模板:一般用在手机在做自适应的情况. 第一步:需要添加判断是否是手机访问的方法: http://www.cnblogs.com/e0yu/p/7561811.html 第二步:Home / ...

  8. 第k最短路A*启发式搜索

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21549   Accepted: 5862 ...

  9. Windows使用filezilla搭建FTP服务器

    参考:https://segmentfault.com/a/1190000009033181 下载软件https://filezilla-project.org/ 安装过程不详述,默认安装即可 启动软 ...

  10. poj2185 Milking Grid【KMP】

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10084   Accepted: 4371 Des ...