Two shortest

Time Limit: 500ms
Memory Limit: 4096KB

This problem will be judged on SGU. Original ID: 185
64-bit integer IO format: %I64d      Java class name: Solution

 
Yesterday Vasya and Petya quarreled badly, and now they don't want to see each other on their way to school. The problem is that they live in one and the same house, leave the house at the same time and go at the same speed by the shortest road. Neither of them wants to change their principles, that is why they want to find two separate shortest routes, which won't make them go along one road, but still they can meet at any junction. They ask you to help them. They number all the junctions with numbers from 1 to N (home and school are also considered as junctions). So their house has the number 1 and the school has the number N, each road connects two junctions exactly, and there cannot be several roads between any two junctions.

 

Input

The first line contains two integer numbers N and M (2<=N<=400), where M is the number of roads Petya and Vasya noticed. Each of the following M lines contains 3 integers: X, Y and L (1<=X, Y<=N, 1<=L<=10000), where X and Y - numbers of junctions, connected by the road and L is the length of the road.

 

Output

Write to the first line numbers of the junctions in the way they passed them on the first route. Write to the second line numbers of the junctions in the way they passed them on the second route. If it is impossible to help guys, then output "No solution".

 

Sample Input

Sample test(s)
Input
 
 
6 8
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2
 
 
Output
 
 
1 3 4 5 6
1 2 4 6
 
 

Source

 
解题:妈拉个巴子,写了三遍才过。。
 
 #include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[maxn*maxn*];
int hd[maxn],hd2[maxn],d[maxn],cur[maxn],tot,n,m;
void add(int *head,int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
void dijkstra() {
priority_queue<pii,vector<pii>,greater<pii > >q;
memset(d,0x3f,sizeof d);
bool done[maxn] = {false};
q.push(pii(d[] = ,));
while(!q.empty()) {
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = hd[u]; ~i; i = e[i].next) {
if(d[e[i].to] > d[u] + e[i].w) {
d[e[i].to] = d[u] + e[i].w;
q.push(pii(d[e[i].to],e[i].to));
}
}
}
}
bool bfs() {
queue<int>q;
memset(d,-,sizeof d);
d[] = ;
q.push();
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = hd2[u]; ~i; i = e[i].next) {
if(e[i].w && d[e[i].to] == -) {
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[n] > -;
}
int dfs(int u,int low) {
if(u == n) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].w && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(low,e[i].w)))) {
e[i].w -= a;
e[i^].w += a;
tmp += a;
low -= a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic() {
int ret = ;
while(bfs()) {
memcpy(cur,hd2,sizeof cur);
ret += dfs(,INF);
}
return ret;
}
void solve(int u) {
if(u == ) printf("%d",u);
else printf(" %d",u);
if(u == n) {
putchar('\n');
return;
}
for(int i = hd2[u]; ~i; i = e[i].next) {
if((~i&) && !e[i].w) {
e[i].w = ;
solve(e[i].to);
break;
}
}
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&m)) {
memset(hd,-,sizeof hd);
memset(hd2,-,sizeof hd2);
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(hd,u,v,w);
add(hd,v,u,w);
}
dijkstra();
for(int i = ; i <= n; ++i) {
for(int j = hd[i]; ~j; j = e[j].next) {
if(d[e[j].to] == d[i] + e[j].w) {
add(hd2,i,e[j].to,);
add(hd2,e[j].to,i,);
}
}
}
if(dinic() >= ) {
solve();
solve();
} else puts("No solution");
}
return ;
}

SGU 185 Two shortest的更多相关文章

  1. SGU 185 Two shortest 最短路+最大流

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...

  2. SGU 185 Two shortest ★(最短路+网络流)

    [题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...

  3. SGU 185.Two shortest (最小费用最大流)

    时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...

  4. sgu 185 最短路建网络流

    题目:给出一个图,从图中找出两条最短路,使得边不重复. 分析:既然是最短路,那么,两条路径上的所有节点的入边(s,x).出边(x,e)必定是最优的,即 dis[x] = dis[s]+edge_dis ...

  5. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  6. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  7. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  8. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  9. Mango Weekly Training Round #6 解题报告

    比赛链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41856#overview A.多种解法.可以dfs倒序染色,如mathlove ...

随机推荐

  1. use 在php 用法中的总结

    1.命名空间 2.匿名函数 3.多继承 4.暂时想到这三个,如果有请补充在评论区

  2. 约瑟夫环问题的链表解法和数学解法(PHP)

    约瑟夫环问题 一群猴子排成一圈.按1,2,-,n依次编号.然后从第1仅仅開始数,数到第m仅仅,把它踢出圈.从它后面再開始数,再数到第m仅仅.在把它踢出去-.如此不停的进行下去.直到最后仅仅剩下一仅仅猴 ...

  3. c19---指针和字符串

    // // main.c // 指针和字符串 // // Created by xiaomage on 15/6/14. // Copyright (c) 2015年 xiaomage. All ri ...

  4. 国内物联网平台初探(一) ——百度物接入IoT Hub

    物接入IoT Hub - 架构 全托管的云服务,帮助建立设备与云端之间安全可靠的双向连接 支撑海量设备的数据收集.监控.故障预测等各种物联网场景 物接入IoT Hub - 功能 通信协议:支持MQTT ...

  5. SwiftUI 官方教程(四)

    SwiftUI 官方教程(四) 4. 自定义 Image View 搞定名称和位置 view 后,我们来给地标添加图片. 这不需要添加很多代码,只需要创建一个自定义 view,然后给图片加上遮罩.边框 ...

  6. B - Guess a number!

    Problem description A TV show called "Guess a number!" is gathering popularity. The whole ...

  7. 基于ACE的TAO开发---一个简单的入门实例-----VS2008(一)

    万事开头难,不管做什么事最开始总是最困难的,一旦上手了就好了. 这也是我自己学习corba编程的一点经验和心得.下面的例子主要是保证读者跟着走能立马看到效果. 1.机器上的TAO是实现已经装好的开发版 ...

  8. Python快速定位工作目录

    原文链接:http://www.cnblogs.com/wdong/archive/2010/08/19/1802951.html 常年奋斗在编码一线的同学,应该都深有体会,工作久了,很多项目文件.技 ...

  9. Sublime + Chrome 本地调试 CSS 选择器

    生成简单的 HTML 代码 使用MacDown写 Markdown,快捷键ALT+CMD+C拷贝成 HTML 代码 粘贴到 Sublime 中,加上 body 标签和 css 头 html <h ...

  10. codeforce 788 A. Funtions again

    链接 A. Functions again 题意 这是一道求最大连续子序列和变形题. 做法 先将abs(a[i+1]-a[i]算出来,然后用两个数组dp[i],cp[i],dp维护其最大值,cp维护其 ...