嘟嘟嘟




题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小。




费用流板子题。

发个博客证明一下我写了这题。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 70;
const int maxm = 1e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m, s, t;
struct Edge
{
int nxt, from, to, cap, c;
}e[maxm << 1];
int head[maxn], ecnt = -1;
void addEdge(int x, int y, int w, int c)
{
e[++ecnt] = (Edge){head[x], x, y, w, c};
head[x] = ecnt;
e[++ecnt] = (Edge){head[y], y, x, 0, -c};
head[y] = ecnt;
} queue<int> q;
bool in[maxn];
int dis[maxn], pre[maxn], flow[maxn];
bool spfa()
{
Mem(in, 0); Mem(dis, 0x3f);
in[s] = 1; dis[s] = 0; flow[s] = INF;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop(); in[now] = 0;
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
v = e[i].to;
if(e[i].cap && dis[now] + e[i].c < dis[v])
{
dis[v] = dis[now] + e[i].c;
pre[v] = i;
flow[v] = min(flow[now], e[i].cap);
if(!in[v]) in[v] = 1, q.push(v);
}
}
}
return dis[t] != INF;
}
int maxFlow = 0, minCost = 0;
void update()
{
int x = t;
while(x != s)
{
int i = pre[x];
e[i].cap -= flow[t];
e[i ^ 1].cap += flow[t];
x = e[i].from;
}
maxFlow += flow[t]; minCost += dis[t] * flow[t];
}
void MCMF()
{
while(spfa()) update();
} void init()
{
Mem(head, -1); ecnt = -1;
maxFlow = minCost = 0;
} int main()
{
int T = 0;
while(scanf("%d%d", &n, &m) != EOF && n && m)
{
init();
s = 0; t = n + 1;
for(int i = 1; i <= m; ++i)
{
int x = read() + 1, y = read() + 1, c = read();
addEdge(x, y, 1, c);
}
addEdge(s, 1, 2, 0); addEdge(n, t, 2, 0);
MCMF();
printf("Instance #%d: ", ++T);
if(maxFlow < 2) puts("Not possible");
else write(minCost), enter;
}
return 0;
}

POJ3068 "Shortest" pair of paths的更多相关文章

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

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

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

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

  3. poj 3068 "Shortest" pair of paths

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

  4. "Shortest" pair of paths[题解]

    "Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...

  5. POJ3068:"Shortest" pair of paths——题解

    http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...

  6. UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解

    题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...

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

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

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

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

  9. UVALIVE 2927 "Shortest" pair of paths

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

随机推荐

  1. OpenFileDialog 打开文件对话框

    InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*" Filt ...

  2. Winform学习之随笔一:Log4net

    前提题要:因为我最近负责的Winform项目,好多都用到了这个log4net的日志功能,开发程序对数据一般都要求做到雁过留痕,所以日志对于我们程序员是不可或缺.因此我把对log4net的使用做一个记录 ...

  3. 7. Reverse Integer 反向输出整数 easy 9. Palindrome Number 判断是否是水仙花数 easy

    Given a 32-bit signed integer, reverse digits of an integer. 将32位整数反向输出. Note:Assume we are dealing ...

  4. java核心技术-IO

    一 .IO 1.1 流的简单介绍和分类 Java流操作的相关的类和接口: Java流类图结构: 四个抽象基类分别为:InputStream .OutputStream .Reader .Writer: ...

  5. ES6学习笔记(五)-函数扩展

  6. 使用匿名函数给setInterval()传递参数

    在使用JScript的时候,我们有时需要间隔的执行一个方法,比如用来产生网页UI动画特效啥的.这是我们常常会使用方法setInterval或setTimeout,但是由于这两个方法是由脚本宿主模拟出来 ...

  7. 【转载】windows 下重置 mysql 的 root 密码

      今天发现 WordPress 连接不上数据库,登录 window server 服务器查看,所有服务均运行正常. 使用 root 账号登录 mysql 数据库,结果提示密码不匹配.我突然意识到,服 ...

  8. 淘宝 NPM 镜像

    使用说明 : 更多见  https://npm.taobao.org 你可以使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm: $ npm install -g cnpm ...

  9. Directly output the object name

    package basic.java; public class Case { public static void main(String[] args) { Student s = new Stu ...

  10. java笔记--增加虚拟机内存

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877243.html "谢谢-- 为避免大型应用程序因虚拟机内存不足而无法 ...