UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有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(最小费用最大流)题解的更多相关文章
- [poj] 3068 "Shortest" pair of paths || 最小费用最大流
		[原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ... 
- UVALIVE 2927 "Shortest" pair of paths
		裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ... 
- 2018.06.27"Shortest" pair of paths(费用流)
		"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ... 
- POJ3068 "Shortest" pair of paths 【费用流】
		POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ... 
- TZOJ 4712 Double Shortest Paths(最小费用最大流)
		描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ... 
- POJ 3068 "Shortest" pair of paths(费用流)
		[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ... 
- P3381 【模板】最小费用最大流 题解
		CSDN同步 原题链接 前置知识: 从三种算法剖析网络流本质 简要题意: 给定网络图,求图的最大流,以及流量为最大流时的最小费用. 现在假设你们看了那篇网络流博客之后,所有人都会了 \(\text{E ... 
- 连续最短路算法(Successive Shortest Path)(最小费用最大流)
		#include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ... 
- UVA 1658 海军上将(拆点法+最小费用限制流)
		海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ... 
随机推荐
- 【Java nio】Channel
			package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ... 
- JZOJ.5325【NOIP2017模拟8.21】九九归一
			Description 
- sencha touch 入门系列 (六)sencha touch运行及代码解析(下)
			接着上一讲,通过index.html里development.js对app.json里js资源文件的解析,app.js便被index.html引入了, app.js是整个项目的程序入口,在项目完成时使 ... 
- Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”
			报错信息: 安装.NET Framework 4时,提示安装未成功,“灾难性故障”.服务器的操作系统是windows server 2008 R2. 查看系统日志时显示“无法安装 Windows 更 ... 
- intelliij jdea 的file没有setting的解决方法
- [Gradle] 发布构件到本地仓库
			配置 需要发布构件的模块 build.gradle 加入如下配置 apply plugin: 'maven-publish' publishing { publications { mavenJava ... 
- MVC之LayOut布局页
			LayOut布局页,就是相当于WebForm中母版页,实现了一个代码的共用和公共布局的作用. 布局页的使用 (1)添加新项,选择MVC布局页 <!DOCTYPE html> <htm ... 
- poj2185 Milking Grid【KMP】
			Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ... 
- # 释放内存                 filter_res_q_l = filter_res_q_l[-2048:]                 filter_res_a_l = filter_res_a_l[-2048:]
			# 释放内存filter_res_q_l = filter_res_q_l[-2048:]filter_res_a_l = filter_res_a_l[-2048:] 
- python 的 ord()、 chr()、 unichr() 函数
			一. ord() 函数描述ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返 ... 
