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 ...
随机推荐
- Environment类,获取程序所在机器信息
一.属性 CommandLine 获取该进程的命令行.CurrentDirectory 获取或设置当前工作目录的完全限定路径.ExitCode 获取或设置进程的退出代码.HasShutdownSta ...
- tomcat学习笔记1
tomcat是一个开源软件,是由java语言编写的,它工作的话需要运行在jvm虚拟机中,说到jvm不得不说下java这个大名鼎鼎的编程 语言了 java这个编程语言最优秀的特点要数write once ...
- spring(spring boot)笔记
1.查看数据库连接:org.springframework.boot.autoconfigure.jdbc里的public DataSource dataSource() 方法.在这里打断点,可以查看 ...
- Nginx Location配置语法介绍、优先级说明
nginx 语法规则:location [=|~|~*|^~|!~|!~*] /uri/ { … } location匹配的是$document_uri,$document_uri 会随 ...
- 复习后台代码(与前面clentHttp连接网络结合)
package com.zzw.LoginServelt; import java.io.IOException; import java.io.PrintWriter; import javax.s ...
- Delphi For Android 开发笔记-附:如何Delphi中同时实现Windows、Android版的GetModuleFileName函数
在Windows中开发DLL时,经常会需要获取当前DLL所在目录以便读取同目录下的其他文件,而目前Delphi在开发android时,其实没多大必要获取,因为整个工程只有一个so文件,而这个so文件也 ...
- 配置visual studio code进行asp.net core rc2的开发(转载jeffreywu)
1.安装.net core sdk https://github.com/dotnet/cli#installers-and-binaries,根据你的系统选择下载 2.下载vscode的C#扩展插件 ...
- [整理归档]30 common tasks you perform using the GUI that you can do faster in Windows PowerShell
主要内容来自于 http://channel9.msdn.com/Events/TechEd/Australia/2014/DCI316 可以下载PPT以及视频,个人只是整理一下平时常用的 NetWo ...
- 博客导出工具(C++实现,支持sina,csdn,自定义列表)
操作系统:windowAll 编程工具:visual studio 2013 编程语言:VC++ 最近博文更新的较频繁,为了防止账号异常引起csdn博文丢失,所以花了点时间做了个小工具来导出博文,用做 ...
- hdu 2141 Can you find it?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2141 Can you find it? Description Give you three sequ ...