Two shortest
sgu185:http://acm.sgu.ru/problem.php?contest=0&problem=185
题意:找两条最短路径,没有边相交的最短路劲,并且输出路径。
题解:这一题和zoj2760,这两题的基本差不多,但是却用了不同的方法,而且输出dfs也不理解。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#define INF 1000000000
using namespace std;
const int N=;
const int M=;
int dist[N],mp[N][N];
struct Node{
int v;
int f;
int next;
}edge[M];
int n,m,u,v,w,cnt,sx,ex;
int head[N],pre[N];
bool visit[N];
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w){
edge[cnt].v=v;
edge[cnt].f=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].f=;
edge[cnt].v=u;
edge[cnt].next=head[v];
head[v]=cnt++;
}
bool BFS(){
memset(pre,,sizeof(pre));
pre[sx]=;
queue<int>Q;
Q.push(sx);
while(!Q.empty()){
int d=Q.front();
Q.pop();
for(int i=head[d];i!=-;i=edge[i].next ){
if(edge[i].f&&!pre[edge[i].v]){
pre[edge[i].v]=pre[d]+;
Q.push(edge[i].v);
}
}
}
return pre[ex]>;
}
int dinic(int flow,int ps){
int f=flow;
if(ps==ex)return f;
for(int i=head[ps];i!=-;i=edge[i].next){
if(edge[i].f&&pre[edge[i].v]==pre[ps]+){
int a=edge[i].f;
int t=dinic(min(a,flow),edge[i].v);
edge[i].f-=t;
edge[i^].f+=t;
flow-=t;
if(flow<=)break;
} }
if(f-flow<=)pre[ps]=-;
return f-flow;
}
void spfa(){
for(int i=;i<=n;i++){
dist[i]=INF;
visit[i]=;
}
queue<int>Q;
dist[]=;
visit[]=;
Q.push();
while(!Q.empty()){
int u=Q.front();
Q.pop();
visit[u]=;
for(int i=;i<=n;i++){
if(mp[u][i]<INF){
if(dist[u]+mp[u][i]<dist[i]){
dist[i]=dist[u]+mp[u][i];
if(!visit[i]){
visit[i]=;
Q.push(i);
}
}
}
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(dist[i]+mp[i][j]==dist[j]&&mp[i][j]!=INF)
add(i,j,);
}
}
int solve(){
int sum=;
while(BFS()){
sum+=dinic(INF,sx);
} return sum;
}
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^].f!= ||(i&))continue;
int v = edge[i].v;
if(v == fa)continue;
edge[i^].f = ;
dfs(v, u);
return;
}
}
int main(){
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
mp[i][j]=INF;
// mp[i][i]=0;
} for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
if(mp[u][v]>w)
mp[u][v]=mp[v][u]=w;
}
spfa(); sx=,ex=n;
if(solve()<||dist[n]==INF)printf("No solution\n");
else{
dfs(,);
dfs(,);
}
}
return ;
}
Two shortest的更多相关文章
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Leetcode: Encode String with Shortest Length && G面经
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- LeetCode 214 Shortest Palindrome
214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- POJ2001 Shortest Prefixes
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- careercup-数组和字符串1.2
1.2 用C或C++实现void reverse(char *str)函数,即反转一个null结尾的字符串. C++实现代码: #include<iostream> #include< ...
- epoll使用实例说明
之前一直在讲如何epoll如何好用,但是并没有实例来演示epoll的使用,下面我们就看一个服务器端使用epoll监听大量并发链接的例子. 首先看一下epoll的几个函数的介绍.1.epoll_crea ...
- Linux下安装并破解StarUML
下载 官网地址: http://starumlreleases-7a0.kxcdn.com/v2.7.0/StarUML-v2.7.0-64-bit.deb CSDN: http://download ...
- CentOS PHP-5.4.8 编译安装之初体验
1. 下载5.4.8 版本 [root@Test data] wget http://museum.php.net/php5/php-5.4.8.tar.gz 2. 解压 [root@Test php ...
- java Junit 测试中异常处理
错误提示: junit.framework.AssertionFailedError: No tests found in错误解决办法 用junit Test运行后,出现如下的错误:junit.fra ...
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...
- AngularJS track by $index引起的思考
今天写了一段程序,只是一个简答的table数据绑定,但是绑定select的数据之后,发现ng-change事件失去了效果,不知道什么原因. 主要用到的代码如下: <div id="ri ...
- Linq XML
写得比较啰嗦,自己记载备用 1 public class XmlFunction 2 { 3 private static XDocument _doc = new ...
- python隐含的特性
本文源自(http://stackoverflow.com/questions/101268/hidden-features-of-python)希望介绍Python非常有用,而比较忽视的Python ...
- 24种设计模式--命令模式【Command Pattern】
今天讲命令模式,这个模式从名字上看就很简单,命令嘛,老大发命令,小兵执行就是了,确实是这个意思,但是更深化了,用模式来描述真实世界的命令情况.正在看这本书的你,我猜测分为两类:已经工作的和没有工作的, ...