时间限制:0.25s

空间限制:4M

题意:

在n(n<=400)个点的图中,找到并输出两条不想交的最短路。不存在输出“No sulotion”;


Solution:

最小费用最大流

建图与poj 2135 一样,添加S到1的流量为2权为0,n到T的流量为2权为0的边,其它边的流量为1,权为路径长度.

但是这道题麻烦不在要输出最短路,而在仅仅4M的内存上。

由于只有4M,我们最多存上400*400条边.但是图却是一个无向图,朴素的想法是存上400*400*2条边,但是这里内存不够.

所以我们首先要确定记录一条边我们是否使用过,如果使用了使用的是那个方向.

相应的在找到增广路后,把正向反向边的流量改变,把反向边的费用变成负值.

最后按照我们标记过的边dfs,并输出就好了.

总的来说是一道足以加深对最小费用最大流的理解的不错的题!

参考代码:

/*
最小费用最大流算法:
思路:
以费用为权做最短路算法。
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
const int INF = , Maxn = 0x3f3f3f3f; struct node {
int u, v, t, c, next;
} edge[INF * INF];
int head[INF], nCnt = ;
int G[INF][INF];
void addEdge (int u, int v, int traffic, int cost) {
edge[++nCnt].v = v, edge[nCnt].u = u, edge[nCnt].t = traffic, edge[nCnt].c = cost;
edge[nCnt].next = head[u], head[u] = nCnt;
edge[++nCnt].v = u, edge[nCnt].u = v, edge[nCnt].t = traffic, edge[nCnt].c = cost;
edge[nCnt].next = head[v], head[v] = nCnt;
}
int max_flow, min_cost;
int n, m, SS, ST, S, T, min_dis = Maxn;
int SPFA() {
queue<int> ql;
int vis[INF] = {}, dis[INF], pre[INF] = {};
ql.push (SS);
memset (dis, 0x3f, sizeof dis);
vis[SS] = , dis[SS] = ;
while (!ql.empty() ) {
int x = ql.front(); ql.pop();
for (int i = head[x]; i != ; i = edge[i].next) {
if (edge[i].t == ) continue;
int v = edge[i].v, c = edge[i].c;
if (dis[v] > dis[x] + c) {
dis[v] = dis[x] + c;
pre[v] = i;
if (!vis[v])
ql.push (v), vis[v] = ;
}
}
vis[x] = ;
}
min_dis = min (min_dis, dis[ST]);
if (dis[ST] == Maxn) return ;
else {
min_cost += dis[ST];
int k = pre[ST];
int cur_flow = Maxn;
while (k) {
if (cur_flow > edge[k].t) cur_flow = edge[k].t;
G[edge[k].u][edge[k].v] = G[edge[k].v][edge[k].u] = ^ G[edge[k].v][edge[k].u];
edge[k].t = edge[k ^ ].t, edge[k].c = abs (edge[k].c);
edge[k ^ ].t = , edge[k ^ ].c = -abs (edge[k ^ ].c);
k = pre[edge[k].u];
}
max_flow += cur_flow;
k = pre[ST];
while (k) {
edge[k].t -= cur_flow, edge[k ^ ].t += cur_flow;
k = pre[edge[k].u];
}
return ;
}
}
void dfs (int x) {
for (int i = head[x]; i != ; i = edge[i].next) {
if (G[x][edge[i].v] && edge[i].t > && edge[i].v < T) {
edge[i].t = ;
dfs (edge[i].v);
break;
}
}
if (x == S) printf ("%d", x);
else
printf (" %d", x);
}
int MCMF() {
while (SPFA() );
if (max_flow == && min_cost == * min_dis) {
dfs (T);
putchar ();
dfs (T);
}
else
puts ("No solution");
}
void build() {
scanf ("%d %d", &n, &m);
int x, y, z;
for (int i = ; i <= m; i++) {
scanf ("%d %d %d", &x, &y, &z);
addEdge (x, y, , z);
}
S = , T = n;
SS = n + , ST = n + ;
addEdge (SS, S, , ), addEdge (T, ST, , );
}
int main() {
build();
MCMF();
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. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  3. CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)

    题意:2个人从1走到n,假设一条路第一次走则是价值di,假设第二次还走这条路则须要价值di+ai,要你输出2个人到达终点的最小价值! 太水了!一条边建2次就OK了.第一次价值为di,第二次为ai+di ...

  4. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  5. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  6. UVa 10806 Dijkstra,Dijkstra(最小费用最大流)

    裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...

  7. TZOJ 1513 Farm Tour(最小费用最大流)

    描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...

  8. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  9. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

随机推荐

  1. 单独启动tomcat

    原料: jdk-1_5_0_13-windows-i586-p.exe apache-tomcat-5.5.25.zip 安装jdk,路径为:C:\Program Files\Java\jdk1.5. ...

  2. 网络流(费用流)CodeForces 321B:Ciel and Duel

    Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...

  3. 通过UIImagePickerController完成照相和相片的选取

    UIImagePickerController是用于选取现有照片,或者用照相机现场照一张相片使用的 定义: @interface ShowViewController : UIViewControll ...

  4. Hdu 5036-Explosion 传递闭包,bitset,期望/概率

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5036 Explosion Time Limit: 6000/3000 MS (Java/Others)   ...

  5. Windows下父进程监视子进程状态

    最近研究自动化测试,需要获取程序的运行状态及结果,下面是些参考资料. 原文地址:http://blog.csdn.net/ariesjzj/article/details/7226443 Linux下 ...

  6. TCP Connection Establishment and Termination

    Three-Way Handshake The following scenario occurs when a TCP connection is established: The server m ...

  7. java ee eclipse 配置 ssh框架

    mvnDebug tomcat:run 这条命令主要用来远程测试,它会监听远程测试用的8000端口,在eclipse里打开远程测试后,它就会跑起来了,设断点,调试,一切都是这么简单. 0.如果是mav ...

  8. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  9. 403. Frog Jump

    做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们 ...

  10. Android关于ListView中item与控件抢夺焦点的那些事

    在开发中,listview可以说是我们使用最频繁的控件之一了,但是关于listview的各种问题也是很多.当我们使用自定义布局的Listview的时候,如果在item的布局文件里面存在Button或者 ...