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 ... 
随机推荐
- SQL Server使用ROW_NUMBER进行快速分页查询
			DECLARE @pageSize INTDECLARE @pageIndex INT --第4页,每页显示10条数据SET @pageSize = 10SET @pageIndex = 4 SELE ... 
- 利用QVOD架设流媒体服务器/电影服务器/vod服务器
			电影服务器一点也不稀罕,是的我们见的太多了,但是大家有没有想过自己也能架一个这样的服务器? 当然现在架一个电影服务器不切实际,去年吵的闹哄哄的“视听许可证”想必大家有所耳闻,再加上电影对服务器的要求一 ... 
- maven 程序包com.sun.image.codec.jpeg
			在 Pom.xml 增加 <build> <plugins> <plugin> <artifactId>maven-compiler-plugin< ... 
- Unable to update index for central http://repo1.maven.org/maven2/ 解决方法
			不知道什么原因 MyEclipse(eclipse) 中的 maven 插件突然不能用了,修改 pom.xml 无任何反应 控制台报 Unable to update index for centra ... 
- ArcGISPlotSilverlightAPI For WPF
			这两天有个需求,在地图上做标绘箭头,效果如下图. Arcgis for WPF 10.2.5.0版本,然而官方文档中没有这种API,自己去写一个呢,又感觉无从下手.无奈去网上搜索了一下,发现一篇好文: ... 
- INNODB与MyISAM两种表存储引擎区别
			mysql数据库分类为INNODB为MyISAM两种表存储引擎了,两种各有优化在不同类型网站可能选择不同,下面小编为各位介绍mysql更改表引擎INNODB为MyISAM技巧. 常见的mysql表引擎 ... 
- SP从32位系统到64位系统的迁移方法
			前提:目标系统为64位1.在32位系统下正常安装SP,获取SP运行时必须的文件2.将[浪潮集团金融事业部]目录拷贝到目标系统的[C:\Program Files]目录3.进入目标系统的[浪潮集团金融事 ... 
- Android 性能优化的方面方面都在这儿
			又到周六了,鸿洋的不定期的周六放送又来了~~这次来谈谈性能优化吧.大家在工作中或多或少都会拿自家的应用和竞品app做比对,不可避免的需要做一些app性能优化的活.很多时候可能是策略上的调整,不过还是有 ... 
- android 在非UI线程更新UI仍然成功原因深入剖析
			”只能在UI主线程中更新View“. 这句话很熟悉吧? 来来,哥们,看一下下面的例子 @Override protected void onCreate(Bundle savedInsta ... 
- 搭建高可用mongodb集群(三)—— 深入副本集内部机制
			在上一篇文章<搭建高可用mongodb集群(二)-- 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ... 
