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的更多相关文章

  1. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  6. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. 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 ...

  8. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  9. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  10. Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. Makefile详解--隐含规则

    Makefile详解--隐含规则(转) Makefile系列文章,这里有个前辈连续洗了一个系列来介绍,共有26篇博客文章. http://www.cppblog.com/ivenher/archive ...

  2. JDBC Transaction Management Example---reference

    In this post, we want to talk about JDBC Transactions and how we can manage the operations in a data ...

  3. HDU2088JAVA

    Hot~~招聘——巴卡斯(杭州),亚信科技,壹晨仟阳(杭州) Box of Bricks Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: ...

  4. XC一键锁屏应用

    XC一键锁屏,一键Android锁屏应用,彻底解放开关机键~ 下载地址: http://download.csdn.net/detail/jczmdeveloper/7329447

  5. angularjs 更新局部作用域

    前几天项目需要,做了一个背景遮罩的弹出框,html采用js动态添加进去的,结果发现angularjs绑定在这里面不起作用,搜索下解决了,记录下: var smallApplyParent = docu ...

  6. IHttpModule实现URL重写

    1.用自定义IHttpModule实现URL重写 一般来说,要显示一些动态数据总是采用带参数的方式,比如制作一个UserInfo.aspx的动态页面用于显示系统的UserInfo这个用户信息表的数据, ...

  7. web服务构架

    以我的理解大流量电商网站,一般构架如下: CDN 负载均衡集群 < === >  缓存服务器集群 反向代理服务器集群 web服务器集群(日志采集) < === > 缓存系统集群 ...

  8. POJ-3278(BFS)

    题目:                                                                                                 ...

  9. 本地tomcat的start.bat启动时访问不出现小猫图标

    排除端口错误.看看是不是webapps的root文件夹删除了,如果删除了,从tomcat的压缩包中解压一个root文件夹,房里面即可

  10. 一行代码实现headView弹簧拉伸效果

    前言 很多app的个人中心上部的headView都实现了弹簧拉伸的效果,即tableView的top并不随着下拉而滑动,而是紧紧的停在屏幕的最上方. 我们今天就分析一下这个效果的实现方式. 分析 关键 ...