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 ...
随机推荐
- Chrome(谷歌浏览器) 程序开发32个经常使用插件
Chrome(谷歌浏览器) 程序开发32个经常使用插件 谷歌浏览器(Chrome)在2008年底才公布.但非常快它已成为火狐(Firefox)有力竞争对手. 之前. ...
- 怎样使用Eclipse PDT调试PHP程序
本文主要介绍的是怎样用eclipse pdt调试PHP 代码. 1. 下载eclipse.从官网上找就能够了,并确认当前系统中有java环境,即jdk和jre. 2. 安装pdt了,採用的是在线安装. ...
- POJ3463 Sightseeing
题目大意:求两点间最短路与长度为最短路长度+1的路径的条数之和. 方法1:最短路径+DP 首先求出ST间最短路径,然后根据递归式记忆化搜索(因此还要构造反向图). 我们知道到达终点的路径长度最长为ma ...
- Spring SSM 框架
IDEA 整合 SSM 框架学习 http://www.cnblogs.com/wmyskxz/p/8916365.html 认识 Spring 框架 更多详情请点击这里:这里 Spring 框架是 ...
- 2.Ventuz Designer常用工具介绍
Ventuz Designer常用工具介绍 1. 打开Ventuz Designer 图1.1 2. Ventuz Designer第一个界面 图2.1 Recent Projects:最近创建的 ...
- 我的wordpress在Nginx的配置
lnmp生成过程 You select the exist rewrite rule:/usr/local/nginx/conf/wordpress.conf Gracefully shutting ...
- SQL数据库链接代码的解释
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=(local);Initial ...
- python安装Django需要环境
Django==1.10.3 -e git+https://github.com/duoshuo/duoshuo-python-sdk.git#egg=duoshuo-python-sdk djang ...
- QT线程使用收集示例
关于多线程问题: Qt和Boost做跨平台的线程封装,OpenMP主要做并行计算,让不精通多线程的人也能高效地利用CPU的计算能力.个人倾向于用boost.thread, boost.mpi. 一 ...
- tomcat 使用常见问题
tomcat 下目录的介绍: 参考地址: http://blog.csdn.net/u013132035/article/details/54949593 参考书籍:tomcat权威指南 (1) 非w ...