SGU 185 Two shortest
Two shortest
This problem will be judged on SGU. Original ID: 185
64-bit integer IO format: %I64d Java class name: Solution
Input
Output
Sample Input
1 2 1
3 2 1
3 4 1
1 3 2
4 2 2
4 5 1
5 6 1
4 6 2
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的更多相关文章
- SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...
- SGU 185 Two shortest ★(最短路+网络流)
[题意]给出一个图,求 1 -> n的2条 没有重边的最短路. 真◆神题--卡内存卡得我一脸血= =-- [思路] 一开始我的想法是两遍Dijkstra做一次删一次边不就行了么你们还又Dijks ...
- SGU 185.Two shortest (最小费用最大流)
时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...
- sgu 185 最短路建网络流
题目:给出一个图,从图中找出两条最短路,使得边不重复. 分析:既然是最短路,那么,两条路径上的所有节点的入边(s,x).出边(x,e)必定是最优的,即 dis[x] = dis[s]+edge_dis ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- 图论常用算法之一 POJ图论题集【转载】
POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- Mango Weekly Training Round #6 解题报告
比赛链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41856#overview A.多种解法.可以dfs倒序染色,如mathlove ...
随机推荐
- iOS 在 ARC 环境下 dealloc 的使用、理解误区
iOS 在 ARC 环境下 dealloc 的使用.理解误区 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&qu ...
- C语言——结构体的使用
C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体. 1.建立一个结构体 struct 结构体名{ 成员列表 }; struct Date { int month; int da ...
- 怎样才是一个基本水平的java程序员?
怎样才是一个基本水平的java程序员? 熟悉常用的数据结构,包括数组,链表,树,哈希表等. 熟悉结构化编程和面向对象编程. 能够阅读UML设计图,根据UML语义进行编码 了解RDBMS和SQL的使用, ...
- Python笔记(十)——操作SQLServer
#encoding=utf-8 # 先通过如下命令安装模块 # pip install --trusted-host pypi.python.org pymssql # pip类似于RedHat里的y ...
- 异步lambda表达式
- unity3d 让物体移动到点击位置
using UnityEngine; using System.Collections; public class test : MonoBehaviour { //在场景中鼠标点击地面后,角色可以移 ...
- MobX入门
MobX入门 本文尝试解释MobX是如何运作的.我们将用MobX创建一个小案例.如果你正在找靠谱的MobX文档,可以去看官方文档. 什么是MobX 官方文档的解释:简洁,易扩展的状态管理.简单来说,M ...
- Task.Factory.StartNew多线程中将数值实时传递到UI显示
private void button1_Click(object sender, EventArgs e) { Task t1 = Task.Factory.StartNew(() => k1 ...
- css3 flex 详解,可以实现div内容水平垂直居中
先说一下flex一系列属性: 一.flex-direction: (元素排列方向) ※ flex-direction:row (横向从左到右排列==左对齐) ※ flex-direction:row- ...
- LINUX的signal
linux的信号来源 1.由一个进程发给另一个进程(或本身) 2.内核发给进程 信号的特征 异步的,分为可靠信号和不可靠信号. 进程收到信号时怎么处理 1.执行信号处理程序 2.如果收到信号时处在一个 ...