uva 1658 Admiral - 费用流
vjudge传送门[here]
题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小。
正解是吧2到v-1的每个点拆成两个点,中间连一条容量为1,费用为0的边,然后求1到v的流量为2的最小费用流就行了。
Code
/**
* Uva
* Problem#1658
* Accepted
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && ~x);
if(x == -){
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
int cap;
int flow;
int cost;
Edge(const int end = , const int next = , const int cap = , const int flow = , const int cost = ):end(end), next(next), cap(cap), flow(flow), cost(cost){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int cap, int flow, int cost){
edge[++ce] = Edge(end, h[from], cap, flow, cost);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end, int cap, int cost){
addEdge(from, end, cap, , cost);
addEdge(end, from, cap, cap, -cost);
}
Edge& operator [](int pos){
return edge[pos];
}
inline int reverse(int pos){
return (pos & ) ? (pos + ) : (pos - );
}
inline void clean(){
delete[] h;
delete[] edge;
ce = ;
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
/// map template ends int n, m;
MapManager g; inline boolean init(){
if(!readInteger(n)) return false;
readInteger(m);
g = MapManager(n * , m * + n * );
for(int i = , a, b, w; i <= m; i++){
readInteger(a);
readInteger(b);
readInteger(w);
g.addDoubleEdge(a + n, b, , w);
}
for(int i = ; i <= n; i++){ //拆点
g.addDoubleEdge(i, i + n, , );
}
return true;
} int* dis;
boolean* visited;
int* last;
int* laste;
int* mflow;
int cost;
int s, t;
queue<int> que;
int sizee; inline void spfa(){
memset(dis, 0x7f, sizeof(int) * (sizee));
memset(visited, false, sizeof(boolean) * (sizee));
que.push(s);
last[s] = ;
dis[s] = ;
laste[s] = ;
mflow[s] = INF;
visited[s] = true;
while(!que.empty()){
int e = que.front();
que.pop();
visited[e] = false;
for(int i = m_begin(g, e); i != ; i = g[i].next){
int &eu = g[i].end;
if(dis[e] + g[i].cost < dis[eu] && g[i].flow < g[i].cap){
dis[eu] = dis[e] + g[i].cost;
last[eu] = e;
laste[eu] = i;
mflow[eu] = min(g[i].cap - g[i].flow, mflow[e]);
if(!visited[eu] && eu != t){
que.push(eu);
visited[eu] = true;
}
}
}
}
for(int i = t; i != s; i = last[i]){
g[laste[i]].flow += mflow[t];
g[g.reverse(laste[i])].flow -= mflow[t];
cost += mflow[t] * g[laste[i]].cost;
}
} inline void minCostFlow(){
s = + n, t = n, sizee = * n + ;
dis = new int[(const int)(sizee)];
visited = new boolean[(const int)(sizee)];
last = new int[(const int)(sizee)];
laste = new int[(const int)(sizee)];
mflow = new int[(const int)(sizee)];
spfa();
spfa();
} inline void solve(){
cost = ;
minCostFlow();
printf("%d\n", cost);
g.clean();
} int main(){
while(init()){
solve();
}
return ;
}
uva 1658 Admiral - 费用流的更多相关文章
- uva 1658 Admiral (最小费最大流)
uva 1658 Admiral 题目大意:在图中找出两条没有交集的线路,要求这两条线路的费用最小. 解题思路:还是拆点建图的问题. 首先每一个点都要拆成两个点.比如a点拆成a->a'.起点和终 ...
- UVa 1658 Admiral(最小费用最大流)
拆点费用流 --------------------------------------------------------------------- #include<cstdio> # ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UVa 1658 - Admiral(最小费用最大流 + 拆点)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 1658 Admiral (最小费用最大流)
最短路对应费用,路径数量对应流量.为限制点经过次数,拆点为边.跑一次流量为2的最小费用最大流. 最小费用最大流和最大流EK算法是十分相似的,只是把找增广路的部分换成了求费用的最短路. #include ...
- uva 1658 Admiral 【 最小费用最大流 】
拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...
- uva 1658(最小费用最大流)
题意:一个带权有向图,求起点到终点的两条路径权值之和最小,且两条路径没有公共点(除起点,终点): 分析:拆点法,将u拆成u和u',u-u'容量为1,费用为0,这样就能保证每个点只用一次,起点s-s'容 ...
- UVA - 1658 Admiral
3. C - Admiral 题意:给定v(3<=v<=1000)个节点,e(3<=e<=10000)条边的又向加权图,求1->v的两条不相交的路径,使得权和最小. 思路 ...
- UVA 1658 Admiral 海上将军(最小费用流,拆点)
题意: 一个有v个点的有向图,要从点1到点v需要找两条路径,两路径不可经过同一个点(除了1和v点).求这两条路径的最小费用(保证有解). 分析: 难在建图,其他套模板. 此图给的是超级复杂图,两个点之 ...
随机推荐
- CCCC L2-002. 链表去重
https://www.patest.cn/contests/gplt/L2-002 模拟一个链表的去重操作 题解:别模拟了,直接用内置的list和map.关于输出的地址,直接用pair存地址和值,输 ...
- Docker基本命令与使用 —— Dockerfile指令与构建(三)
一.Dockerfile指令上 1.指令格式 # Comment 注释, 以#开头 INSTRUCTION argument 以大写的指令+参数 #First Dockerfile 注释 FROM u ...
- 服务器端FIN的条件
服务器端FIN的条件_域名/网络_常见问题_对象存储 OSS-阿里云 https://help.aliyun.com/knowledge_detail/65427.html 服务器端FIN的条件 KB ...
- SecTools.Org--bp
Burp Suite使用介绍(一) | WooYun知识库 http://drops.wooyun.org/tips/2227 我的渗透利器 | EVILCOS fr ...
- The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
运行环境: Intellij idea 14 在改了项目名称. 运行时候出现了 The APR based Apache Tomcat Native library which allows opti ...
- Roadblocks--poj3255(次短路)
题目链接 求次短路的问题: dist[i][0]和dist[i][1]表示从起点1到i的距离和从起点n到i的距离: 次短路要比最短路大但小于其他路: 每条路1--n的距离都可以用dist[i][0] ...
- 浅谈CSRF攻击方式(转)
add by zhj: 在看Django开发的应用时,看到了CSRF,然后搜到了这篇文章,讲的不错.其实CSRF 攻击也蛮简单的.当你登陆网站A后,会在本地存有cookie,在cookie没有过期的情 ...
- python 面向对象 issubclass
判断是否 他的父类 class Foo(object): pass obj = Foo() class Boo(Foo): pass class Coo(Boo): pass obj = Boo() ...
- 改变wordpress图片上传后的压缩质量
WordPress 在图片上传后会默认压缩图片质量为原来的 90%,这样做的好处可以极大的加快页面的载入速度与缩小图片大小所占服务器空间. 如果希望 100% 原质量怎么办呢?如何禁止 WordPre ...
- 查看crontab的日志记录定位定时任务问题
1.linux 看 /var/log/cron这个文件就可以,可以用tail -f /var/log/cron观察 2.unix 在 /var/spool/cron/tmp文件中,有croutXXX0 ...