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. ...
随机推荐
- JAAS LOGIN IN WEBLOGIC SERVER--reference
The Java Authentication and Authorization Service (JAAS) is a standard extension to the security in ...
- NoteExpress格式化复制指定输出样式
在NoteExpress中没有看到为命令“选中的题录右击 => 复制题录 => 格式化复制”指定输出样式的明确配置项,但格式化复制的输出样式也是可以变化了,随细节大面板里的“预览”标签页里 ...
- Linux网络相关命令小结
# ifconfig # ifup/ifdown # route -n # ip link show //显示本机所有接口信息 # traceroute # netstat //查看本机网络连接与后门 ...
- 在C#中使用正则表达式自动匹配并获取所需要的数据
转自:http://my.oschina.net/bv10000/blog/111736 正则表达式能根据设置匹配各种数据(比如:e-mail地址,电话号码,身份中号码等等).正则表达式功能强大,使用 ...
- SqlSugar常用查询实例-拉姆达表达式
SqlSugar支持拉姆达表达式查询,匿名对象参数等,相对还是比较方便好用的. 一.查询列表: //查询列表 SqlSugarClient db = SugarContext.GetInstance( ...
- log4net日志组件
转载:http://www.cnblogs.com/knowledgesea/archive/2012/04/26/2471414.html 一.什么是log4net组件 Log4net是基于.net ...
- 微信Demo导入遇到的问题
最近做支付宝和微信接入自己APP工程的功能,遇到了一些问题,跟大家分享: 这里先说Android开发微信支付接入. 首先根据官方文档进行,对比支付宝的官方文档,微信部分更显得“摘要”一些. 导入后自行 ...
- java中时间差计算
public class Utill { public String TimeString(Date currentTime, Date beginTime){ /*默认为毫秒,除以1000是为了转换 ...
- Asp.Net Mvc4 Ajax提交数据成功弹框后跳转页面
1.cshtml页面代码 @model Model.UserInfo @{ ViewBag.Title = "Edit"; var options = new AjaxOp ...
- tar 解压缩
解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2 //解压 tar.bz ...