题目链接: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. 导出数据库数据制成Excel和txt

    引用ICSharpCode.SharpZipLib.dll 1.编写压缩和解压代码 using System; using System.Collections.Generic; using Syst ...

  2. CentOS用户权限管理--su与sudo

    Linux权限管理--su与sudo 1.su用来切换登录的用户,比如当前用户为chen,可以用su zhu,并输入用户zhu的登录密码,就可以切换到用户zhu.如果一个普通用户想切换到root用户, ...

  3. Linux基础知识-文件管理

    Linux目录与路径 cd:切换目录 例如:cd ~willhua,则回到用户willhua的主文件夹  cd ~或者cd,则表示回到自己的的主文件夹  cd -,则表示回到上个目录 pwd:显示目前 ...

  4. DM9000网卡的基本工作原理

    MAC:主要负责数据帧的创建,数据差错,检查,传送控制等. PHY:物理接口收发器,当收到MAC过来的数据时,它会加上校验码,然后按照物理层的规则进行数据编码,再发送到传输介质上,接收过程则相反. M ...

  5. 成功完成Moses Manual中BaseLineSystem

    终于把这个破法语句子翻译出来了,各种耗时,bug,弄了一天,明天争取看完详细的翻译教程! 而且还要学习中文分词,晚安,Moses!

  6. DrawTool画笔之图形笔

    相关知识参考DrawTool画笔之纹理笔  , 图形笔的实现跟纹理笔的实现是一样的,重载Stroke的DrawCore方法,效果图: --------------------------------- ...

  7. [整理归档]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 ...

  8. 苏泊尔借助微软CRM提升客户满意度

    企业背景 作为中国最大.全球第二的炊具研发制造商和中国小家电领先品牌,品质和创新一是苏泊尔矢志追求的企业理念,从火红点无油烟锅的发明到能做柴火饭的球釜IH饭煲的面世,苏泊尔用产品的创新和品质的承诺,不 ...

  9. 023使用typeof关键字获取类内部结构

    private void button1_Click(object sender, EventArgs e) { Focus(); string a=txtType.Text; // Type typ ...

  10. [Environment Build] Win10下Appach配置

    1. Apache下载,登录http://httpd.apache.org/download.cgi,选择Files for Microsoft Windows, 有以下几个选择, 我选择的是Apac ...