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 ...
 
随机推荐
- Docker学习之基本概念
			
Docker学习之基本概念 作为一个后端noder,不了解docker有点说不过去,这节开始,学习一些docker层面的东西. 什么是docker Docker最初是dotCloud公司创始人Solo ...
 - 局域网内配置虚拟机的hostname
			
一般上我们在局域网内访问,比如宿主机访问虚拟机的时候可以直接使用IP去访问,大多数情况下也都适用.但是对于有的情况,比如像新版的hbase的配置,它默认将localhost作为hbase.master ...
 - layui——上传图片,并实现放大预览
			
一般上传文件后会返回文件的路径,然后存储到数据库,那么首先实现上传后的放大和删除功能 function uploadSmallPic() { var upload = layui.upload; up ...
 - 撩课-Java每天5道面试题第8天
			
撩课Java+系统架构 视频 点击开始学习 71.jsp有哪些内置对象?作用分别是什么? Page, pageContext, request, response, session, applicat ...
 - Web前端面试指导(十六):为什么要初始化CSS样式?
			
题目点评 这个题目乍一看感觉怪怪的,什么叫初始化样式了?如果换一句话你可能就理解了,就是通用样式.这道题目主要涉及的是理论方面的知识,不用写代码,只要描述清楚就可以了 初始化样式的原因 因为浏览器的兼 ...
 - js中作用域链和作用域
			
作用域 在JavaScript中,我们可以将作用域定义为一套规则,这套规则用来管理引擎如何在当前作用域以及嵌套的子作用域中根据标识符名称(变量名或者函数名)进行变量查找. 经过研究<高级程序设计 ...
 - Spring Data MongoDB 分页查询
			
在上篇文章 Spring Data MongoDB 环境搭建 基础上进行分页查询 定义公用分页参数类,实现 Pageable 接口 import java.io.Serializable; impor ...
 - JAVA - JDK 1.8 API 帮助文档-中文版
			
JAVA - JDK 1.8 API 帮助文档-中文版 百度云链接: https://pan.baidu.com/s/1_7FFadw1a6J0qTfx2FzqPQ 密码: 41n4
 - 一、angularjs基础了解
			
说明:此处比较杂,目前没有统一的总结哦 angularjs 是mvvm框架 加载JS文件总是使用后缀为.min.js的文件,因为这些文件是经过压缩的,能提升应用的启动速度 模块说明: 1.config ...
 - 关于Unity3d的世界空间和本地空间的一点思考
			
最近才开始学Unity3d,被这个本地空间和世界空间搞得有点晕头转向的.于是花了一点时间随便写了一点代码加深理解.代码如下: public class MoveX : MonoBehaviour { ...