POJ3068 "Shortest" pair of paths
嘟嘟嘟
题目大意:一个有向图,每一条边有一个边权,求从节点\(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的更多相关文章
- POJ3068 "Shortest" pair of paths 【费用流】
POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- poj 3068 "Shortest" pair of paths
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1407 ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- POJ3068:"Shortest" pair of paths——题解
http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...
- UVALive - 2927 "Shortest" pair of paths(最小费用最大流)题解
题意:有n个机器,机器之间有m条连线,我们需要判断机器0到n-1是否存在两条线路,存在输出最小费用. 思路:我们把0连接超级源点,n-1连接超级汇点,两者流量都设为2,其他流量设为1,那么只要最后我们 ...
- POJ 3068 "Shortest" pair of paths(费用流)
[题目链接] http://poj.org/problem?id=3068 [题目大意] 给出一张图,要把两个物品从起点运到终点,他们不能运同一条路过 每条路都有一定的费用,求最小费用 [题解] 题目 ...
- [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 ...
随机推荐
- MVC-AOP(面向切面编程)思想-Filter 三种注册方式
在ASP.NET MVC框架中,为我们提供了四种类型的Filter类型包括:IAuthorizationFilter.IActionFilter.IResultFilter.IExceptionFil ...
- jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)
这篇文章主要是对前两篇关于ajaxfileupload.js插件的文章 <ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload. ...
- 五:SpringCloud-Zuul
九:zuul路由网关 1.概述 1.1 是什么 Zuul包含了对请求的路由和过滤两个最主要的功能: 其中==路由功能==负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础. 而==过 ...
- CentOS 7重装mysql8.0.15
查看是否安装有mysql rpm -qa | grep mysql 有的话先删除干净 下载MySQL yum源 wget https://dev.mysql.com/get/mysql80-commu ...
- 为什么要学习 UML?
UML 的首要价值是沟通和理解.好的图形可以帮助沟通设计思想,尤其是要回避许多细节时,图形也可以帮助你理解软件系统或业务流程.作为团队的成员,尝试弄清楚某些东西时,图形有助于理解和沟通整个团队所理解到 ...
- 2.springioc实例化bean的三个方法
1.构造器 也就是在上一篇讲的那个例子,调用默认的无参构造函数 2.静态工厂方法 1)创建需要执行的方法的类 public class HelloWorld { public HelloWorld() ...
- libevent学习笔记 —— 第一个程序:计时器
用libevent写个定时器其实步骤不多: 1.初始化libevent 2.设置事件 3.添加事件 4.进入循环 由于定时事件触发之后,默认自动删除,所以如果要一直计时,则要在回调函数中重新添加定时事 ...
- Luogu4887 第十四分块(前体)
sto \(lxl\) orz 考虑莫队,每次移动端点,我们都要询问区间内和当前数字异或有 \(k\) 个 \(1\) 的数字个数 询问 \([l,r]\) 可以再次离线,拆成询问 \([1,l-1] ...
- 洛谷P3939 数颜色(二分 vector)
题意 题目链接 Sol 直接拿vector维护每种颜色的出现位置,然后二分一下. #include<bits/stdc++.h> using namespace std; const in ...
- Uncaught TypeError: timeout.close is not a function. when try to use clearInterval
It's because of your IDE! Make sure you have added automatic imports such as import { clearInterval ...