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. Coco2d-js/Cocos2d-html5中Android返回键实现

    导语: 首先Cocos2d-x其中实现Menu和Back按键相对简单一点,而在资源较少的Cocos2d-html5其中.要实现返回还是有一点不一样的,并且有没有详细的demo.也就仅仅有自己去看api ...

  2. Xcode6+Cocos2d-x真机调试 报错

    眼下真机调试时遇到下面问题. Undefined symbols for architecture arm64: "_png_get_io_ptr", referenced fro ...

  3. YII用户注冊和用户登录(三)之模型中规则制定和分析

    3 模型中规则制定和分析 YII模型主要分为两类,一个数据模型,处理和数据库相关的增删改查.继承CActiveRecord.还有一个是表单模型,继承CFormModel.不与数据库进行交互.操作与数据 ...

  4. HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)

    题目链接:pid=5128">http://acm.hdu.edu.cn/showproblem.php? pid=5128 题面: The E-pang Palace Time Li ...

  5. bzoj4247: 挂饰(背包)

    4247: 挂饰 题目:传送门 题解: 看完题目很明显的一道二维背包(一开始还推错了) 设f[i][j]表示前i个挂饰选完(可以有不选)之后还剩下j个挂钩的最大值(j最多贡献为n) 那么f[i][j] ...

  6. Swift - 将Data数据转换为[UInt8](bytes字节数组)

    有时上传或者发送图片.文字时,需要将数据转换为 bytes 字节数组.下面介绍两种将 Data 转换为 [UInt8] 的方法. 假设我们有如下 Data 数据要转换: 1 let data = &q ...

  7. jquery的this和$(this)

    1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的  $()  这 ...

  8. My first blog for java

    我的第一个java程序: package com.hellojava; /** * @author 沽-名-钓-誉 */ public class HelloJava{ /** * @param 输出 ...

  9. pythonOCC版 瓶子代码

    #!/usr/bin/env python # -*- coding:utf-8 -*- ##Copyright 2009-2015 Thomas Paviot (tpaviot@gmail.com) ...

  10. IO编程 - 转载自廖雪峰的博文

    IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. 比如你打开 ...