SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068
Yesterday Vasya and Petya quarreled badly, and now they don't want to see each other on their way to school. The problem is that they live in one and the same house, leave the house at the same time and go at the same speed by the shortest road. Neither of them wants to change their principles, that is why they want to find two separate shortest routes, which won't make them go along one road, but still they can meet at any junction. They ask you to help them. They number all the junctions with numbers from 1 to N (home and school are also considered as junctions). So their house has the number 1 and the school has the number N, each road connects two junctions exactly, and there cannot be several roads between any two junctions.
题意描述:有n个节点,找出1~n的两条不相交的最短路(两条路径可以共点不能共边)。
算法分析:用spfa求出点1到各个点的最短路径,然后再if(d[j]==d[i]+dis[i][j]) 来判断边dis[i][j]是否是在最短路径上,如果是就加到图里,结果就得到以1为顶点的最短路径树,1到树中任意一点的连线都是最短路径,首先把这些边加到网络流的边集中,容量为1。跑一遍1到n的最大流,如果流量>=2则有解,再从原点深搜路径即可。
说明:这道题很卡内存和时间,MLE!MLE!MLE!M出翔了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define inf 10737418
using namespace std;
typedef long long ll;
const int maxn=+;
const int M = +; int n,m,from,to;
struct Edge
{
int to,cap;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int cap)
{
Edge E={v,cap,head[u]};
edge[edgenum]=E;
head[u]=edgenum++; Edge E2={u,,head[v]};
edge[edgenum]=E2;
head[v]=edgenum++;
} int d[maxn];
bool BFS()
{
memset(d,-,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty() ){
int u=Q.front(); Q.pop();
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (d[v]==- && edge[i].cap)
{
d[v]=d[u]+,Q.push(v);
if (d[to] != -) return true;
}
}
}
return false;
}
int Stack[maxn], top, cur[maxn];
int Dinic(){
int ans=;
while(BFS())
{
memcpy(cur, head, sizeof(head));
int u = from; top = ;
while()
{
if(u == to)
{
int flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
for(int i = ; i < top; i++)
if(flow > edge[ Stack[i] ].cap)
{
flow = edge[Stack[i]].cap;
loc = i;
} for(int i = ; i < top; i++)
{
edge[ Stack[i] ].cap -= flow;
edge[Stack[i]^].cap += flow;
}
ans += flow;
top = loc;
u = edge[Stack[top]^].to;
}
for(int i = cur[u]; i!=-; cur[u] = i = edge[i].next)//cur[u] 表示u所在能增广的边的下标
if(edge[i].cap && (d[u] + == d[ edge[i].to ]))break;
if(cur[u] != -)
{
Stack[top++] = cur[u];
u = edge[ cur[u] ].to;
}
else
{
if( top == )break;
d[u] = -;
u = edge[ Stack[--top]^ ].to;
}
}
}
return ans;
}
int dis[maxn][maxn];
int vis[maxn];
void spfa()
{
for (int i= ;i<=n ;i++) d[i]=inf;
d[]=;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push();
vis[]=;
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
vis[u]=;
for (int v= ;v<=n ;v++)
{
if (d[v]>d[u]+dis[u][v])
{
d[v]=d[u]+dis[u][v];
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
}
void dfs(int u, int fa)
{
if (u == n){printf("%d\n",u);return ;}
else printf("%d ",u);
for (int i = head[u] ;i!=- ;i=edge[i].next)
{
if (edge[i^].cap != || (i&)) continue;
int v=edge[i].to;
if (v == fa) continue;
edge[i^].cap=;
dfs(v, u);
return;
}
} int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
int a,b,c;
for (int i= ;i<=n ;i++)
for (int j= ;j<=n ;j++)
dis[i][j]=inf;
for (int i= ;i<m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
dis[a][b] = dis[b][a] = min(dis[a][b], c);
}
spfa();
if (d[n]==inf) {printf("No solution\n");continue; }
from=;
to=n+;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=n ;j++)
{
if(dis[i][j]!=inf && d[j] == dis[i][j] + d[i])
add(i,j,);
}
}
add(n,to,);
int sum=Dinic();
if (sum<) {printf("No solution\n");continue; }
dfs(,);
dfs(,);
}
return ;
}
SGU 185 Two shortest 最短路+最大流的更多相关文章
- SGU 185 Two shortest ★(最短路+网络流)
[题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...
- SGU 185.Two shortest (最小费用最大流)
时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...
- SGU 185 Two shortest
Two shortest Time Limit: 500ms Memory Limit: 4096KB This problem will be judged on SGU. Original ID: ...
- hdu5294||2015多校联合第一场1007 最短路+最大流
http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...
- P3376 【模板】网络最大流——————Q - Marriage Match IV(最短路&最大流)
第一道题是模板题,下面主要是两种模板,但都用的是Dinic算法(第二个题也是) 第一题: 题意就不需要讲了,直接上代码: vector代码: 1 //invalid types 'int[int]' ...
- BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )
最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...
- 【最短路+最大流】上学路线@安徽OI2006
目录 [最短路+最大流]上学路线@安徽OI2006 PROBLEM SOLUTION CODE [最短路+最大流]上学路线@安徽OI2006 PROBLEM 洛谷P4300 SOLUTION 先在原图 ...
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
- sgu 185 最短路建网络流
题目:给出一个图,从图中找出两条最短路,使得边不重复. 分析:既然是最短路,那么,两条路径上的所有节点的入边(s,x).出边(x,e)必定是最优的,即 dis[x] = dis[s]+edge_dis ...
随机推荐
- Python生态环境简介[转]
Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy 原译: Python生态环境简介 当 ...
- Vue.js学习 Item1 --快速入门
我们以 Vue 数据绑定的快速导览开始.如果你对高级概述更感兴趣,可查看这篇博文. 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.在浏览器新标签页中打开它,跟 ...
- nodejs服务器anywhere简介
一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g 执行 $ anywhere // or with port $ anywhere ...
- 生成不重复随机数,int转 TCHAR 打印输出
在0~n 中 随机去除不重复的k个数 int k=100; int n=80000; for(int i=0;k>0&&i<n;i++) { if((bigrand()%( ...
- PHP 把GBK编码转换为UTF8
//把GBK编码转换为UTF8 $name="勿以善小而不为"; $name=iconv("GBK", "UTF-8", $name);
- 区间 (vijos 1439) 题解
[问题描述] 现给定n个闭区间[ai,bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间的方案.你的输出应该按照区间的升序排 ...
- R语言中判断是否是整数。以及读写excel
今天接手一个重复性工作, 需要手工把产品运营们在excel里写的活动规则, 插入数据库表中.为了减少出错, 提高效率. 再加上最近刚刚学R语言, 就用R练练手, 自动生成mysql的sql语句. 一次 ...
- 6.python字符串-内置方法列举
所谓内置方法,就是凡是字符串都能用的方法,这个方法在创建字符串的类中,下面是总结: 首先,我们要学习一个获取帮助的内置函数 help(对象) ,对象可以是一个我们创建出来的,也可以是创建对象的那个类, ...
- 010-python基础-数据类型-字符串操作
1.移除空白 username.strip() 2.分割 names = "alex,jack,rain" names_1 = names.split(",") ...
- linux内核设计与实现学习笔记-模块
模块 1.概念: 如果让LINUX Kernel单独运行在一个保护区域,那么LINUX Kernel就成为了“单内核”. LINUX Kernel是组件模式的,所谓组件模式是指:LINUX K ...