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. ...
随机推荐
- End-to-End Tracing of Ajax/Java Applications Using DTrace
End-to-End Tracing of Ajax/Java Applications Using DTrace By Amit Hurvitz, July 2007 Aja ...
- 《Linux内核修炼之道》 系列
http://blog.csdn.net/fudan_abc/article/category/655796
- 学习PHP时的一些总结(二)
类中的构造方法和析构方法: 构造方法是对象创建完成后第一个被对象自动调用的方法.析构方法是对象在销毁之前最后一个被对象自动调用的方法. 如果没有显示的声明构造方法,类中都会默认存在一个没有参数列表并且 ...
- CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
- Android(java)学习笔记195:三重for循环的优化(Java面试题)
1.首先我们看一段代码: for(int i=0;i<1000;i++){ for(int j=0;j<100;j++){ for(int k=0;k<10;k++){ testFu ...
- chmod -R o+rX /data
When using chmod -R o+rx /data , you set the execute permission on all directories as well as files ...
- phpcms 换域名
修改/caches/configs/system.php里面所有和域名有关的,把以前的老域名修改为新域名就可以了. 进行后台设置->站点管理 对相应的站点的域名进行修改. 更新系统缓存.点击 ...
- Linux只iptables
1. 查看<strong>网络</strong>监听的端口: netstat -tunlp 2. 查看本机的路由规则: route stack@ubuntu:~$ route ...
- a标签的简单用法
1.href="#"的作用:页面中有滚动,可以直接回到顶部. <a href="#">回到最顶端</a> 2.href="ur ...
- 导出excel的简单方法
excel的操作,最常用的就是导出和导入,废话不多说上代码. 本例使用NPOI实现的,不喜勿喷哈.... /// <summary> /// 导出Excel /// </summar ...