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. ...
随机推荐
- hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
错误: 14/04/29 02:45:07 INFO mapreduce.Job: Job job_1398704073313_0021 failed with state FAILED due to ...
- java String的比较,BOX装箱拆箱,以及面向对象的小代码
package cn.hncu.day2; public class StringDemo { public static void main(String[] args) { String str1 ...
- OKHttp的简单使用
一方面,最近关于OKHttp的讨论甚嚣尘上,另一方面,我最近也更新了android6.0,发现在6.0中HttpClient不能使用了,于是决定抽时间也看一下OKHttp,总结了一点东西,与大家分享. ...
- 仿QQ迷你首页(VC++,MFC)(迷你资讯)的开发与实现(源代码)
由于需求,需要写个类似QQ迷你资讯首页的东西,就花了一点时间写了写,软件效果截图如下: 程序的主要核心代码如下: 程序的全部源代码下载地址:http://download.csdn.net/downl ...
- Chapter 5. The Gradle Wrapper 关于gradle wrapper
Most tools require installation on your computer before you can use them. If the installation is eas ...
- Bootstrap--全局css样式之表单
单独的表单控件会被自动赋予一些全局样式.所有设置了 .form-control 类的 <input>.<textarea> 和 <select> 元素都将被默认设置 ...
- CI框架篇之控制器篇--设置路由(1)
CodeIgniter 定义默认控制器 当你的网站不存在某个URI 或者 用户直接从根目录访问的时候,CodeIgniter 会加载默认控制器. 打开 application/config/route ...
- H TML5 之 (2) 小试牛刀
基本的HTML都认识到了,就开始运用下了,大的程序的开始,都是从一个一个表达式慢慢的堆积起来的 想开始玩HTML5,就开始理解,它所提供的画布函数各有什么作用,API的具体使用,才能完成自己想要完成的 ...
- Asp.Net Remove Unwanted Headers
原文:http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx 原文:h ...
- TP-LINK wr703n openwrt 挂载 U盘
1.首先设置好DNS 2.点SYSTEM 点SOFTWARE 更新软件列表 3.安装下列软件: block-mount kmod-usb-storage kmod-fs-ext4 e2fsprogs ...