题目链接: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 最短路+最大流的更多相关文章

  1. SGU 185 Two shortest ★(最短路+网络流)

    [题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...

  2. SGU 185.Two shortest (最小费用最大流)

    时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...

  3. SGU 185 Two shortest

    Two shortest Time Limit: 500ms Memory Limit: 4096KB This problem will be judged on SGU. Original ID: ...

  4. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  5. P3376 【模板】网络最大流——————Q - Marriage Match IV(最短路&最大流)

    第一道题是模板题,下面主要是两种模板,但都用的是Dinic算法(第二个题也是) 第一题: 题意就不需要讲了,直接上代码: vector代码: 1 //invalid types 'int[int]' ...

  6. BZOJ 3931: [CQOI2015]网络吞吐量( 最短路 + 最大流 )

    最短路 + 最大流 , 没什么好说的... 因为long long WA 了两次.... ------------------------------------------------------- ...

  7. 【最短路+最大流】上学路线@安徽OI2006

    目录 [最短路+最大流]上学路线@安徽OI2006 PROBLEM SOLUTION CODE [最短路+最大流]上学路线@安徽OI2006 PROBLEM 洛谷P4300 SOLUTION 先在原图 ...

  8. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  9. sgu 185 最短路建网络流

    题目:给出一个图,从图中找出两条最短路,使得边不重复. 分析:既然是最短路,那么,两条路径上的所有节点的入边(s,x).出边(x,e)必定是最优的,即 dis[x] = dis[s]+edge_dis ...

随机推荐

  1. [leetcode]_K Sum 问题

    问题:K Sum问题是一个问题系列,在一个数组中找K个数的和能够满足题目中要求.从2 Sum 到 3 Sum , 3 Sum Clozet , 4 Sum..解法虽一开始不容易想到,但get到解题技能 ...

  2. 两种会话状态之Session会话

    什么是Session 使用Cookie和附加URL参数都可以将上一次请求的状态信息传递到下一次请求中,但是如果传递的状态信息较多,将极大降低网络传输效率和增大服务器端程序处理的难度. Session技 ...

  3. AeroSpike 资料

    文档总览:http://www.aerospike.com/docs/ JAVA AeroSpike知识总览:http://www.aerospike.com/docs/client/java/sta ...

  4. [Linux] Ubuntu Server 12.04 LTS 平台上搭建WordPress(Nginx+MySql+PHP) Part II

    接着上一节继续搭建我们的LNMP平台,接下来我们安装PHP相关的服务 sudo apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5- ...

  5. 【转】MYISAM表批量压缩

    关于对MYISAM表的压缩,可以使用myisampack和myisamchk完成(myisampack完之后必须进行myisamchk才能使用压缩后的表,而且是只读的), 其详细地用法可以参考官方文档 ...

  6. Python学习教程(learning Python)--3.3.4 Python的if-elif-else语句

    Python的if-elif-else语句用于多种条件判断后选择某个语句块执行.该语句可以利用一系列条件表达式进行检查,并在某个表达式为真的情况下执行相应的代码.需要注意的是,虽然if/elif/el ...

  7. windows下python+flask环境配置详细图文教程

    本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+fla ...

  8. [Letcode] 1. Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. 2天驾驭DIV+CSS (基础篇)(转)

    这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” 将你引入两大误区 ...

  10. 7.Knockout.Js(Mapping插件)

    前言 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些属性是observable的,你可以使用KO绑定他们到你的UI元素上,当这些o ...