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 ...
随机推荐
- hive到hbase的使用
一.简单介绍 hive的元数据保存在metastore里面,真实的数据一般位于hdfs中,可以通过hql来对数据进行分析.hbase中的数据也是存放在hdfs上的,可不可以使用hive来分析hbase ...
- PHP四大安全策略
PHP中的文件系统安全.数据库安全.用户数据安全等安全相关的问题. 一.文件系统安全 php如果具有root权限,且在脚本中允许用户删除文件,那么用户提交数据,不进行过滤,就非常有可能删除系统文件 & ...
- LotusPhp起步:经典的HelloWorld
写了几篇LotusPhp,一直没有跑个程序,感觉好像步骤有点错,所以先上个经典的Demo,HelloWorld吧 先按推荐目录建好文件夹,如果懒的建,下面有下载的Demo包,解压就可以用,因为简单,也 ...
- 《安全参考》HACKCTO-201312-12
小编的话 “忽如一夜春风来,千树万树梨花开.” 小伙伴们,不要只为了“千树万树的梨花”而惊喜,陶醉! 与此同时,您最爱的整合型信息安全技术期刊<安全参考>第12期也如约而至啦! 这一期&l ...
- 7-ZIP实现数据高度压缩
From:http://www.cnblogs.com/killerlegend/p/3746395.html Author:KillerLegend Date:2013.5.22 选中文件,鼠标右键 ...
- delphi 基础之二 面向对象概念初步
面向对象概念初步 •类自动生成 快捷键:ctrl+shift+c 1.类的定义 类是用户创建的数据类型,包括状态.表达式和一些操作.有3个组成部分,即字段.方法和属性.字段是类的内部数据变量,方法就是 ...
- Delphi 7 里没有加载的控件
在原来版本如D5.D6中使用的控件如Quickrep,FastNet等,在D7中仍然是保留的.只是Delphi没有将他们默认的安装到组件面版中来.这些控件包全部保存在Delphi目录的bin下,文件扩 ...
- Oracle sql trace
一.SQL_TRACE说明 1.1.在全局启用 在参数文件(pfile/spfile)中指定:sql_trace =true 1.2.在当前session级设置 启用当前session的跟踪: alt ...
- 第七节:使用实现了dispose模式的类型
知道类型如何实现dispose模式之后,接下来看一下开发人员怎样使用提供了dispose模式的类型.这里不再讨论前面的SafeHandle类,而是讨论更常用的FileStream类. 可以利用File ...
- SQLite之读取数据库内容
1.打开已有数据库. //打开数据库 - (BOOL )openDB {// 红色部分修改为自己的数据库路径 return (SQLITE_OK == sqlite3_open([@"/Us ...