UVALive - 6266 Admiral 费用流
题意:找两条完全不相交不重复的路使得权值和最小。
思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用为0的边,起点和终点容量为2,两点之间有边就加一条容量为1,费用为权值的边,这样跑一边费用流就可以了。
#pragma comment(linker, "/STACK:1000000000")
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
using namespace std;
#define MAXN 9999
#define V 2005
#define E 30005
int vis[V];
int dist[V];
int pre[V]; struct Edge{
int u,v,c,cost,next;
}edge[E];
int head[V],cnt; void init(){
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int c,int cost)
{
edge[cnt].u=u;edge[cnt].v=v;edge[cnt].cost=cost;
edge[cnt].c=c;edge[cnt].next=head[u];head[u]=cnt++; edge[cnt].u=v;edge[cnt].v=u;edge[cnt].cost=-cost;
edge[cnt].c=;edge[cnt].next=head[v];head[v]=cnt++;
} bool spfa(int begin,int end){
int u,v;
queue<int> q;
for(int i=;i<=end+;i++){
pre[i] = -;
vis[i] = ;
dist[i] = INF;
}
vis[begin]=;
dist[begin]=;
q.push(begin);
while(!q.empty()){
u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
if(edge[i].c>){
v=edge[i].v;
if(dist[v]>dist[u]+edge[i].cost){
dist[v]=dist[u]+edge[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
}
return dist[end] != INF;
} int MCMF(int begin,int end){
int ans=,flow;
int flow_sum=;
while(spfa(begin,end)){
flow=INF;
for(int i=pre[end];i!=-;i=pre[edge[i].u])
if(edge[i].c<flow)
flow=edge[i].c;
for(int i=pre[end];i!=-;i=pre[edge[i].u]){
edge[i].c-=flow;
edge[i^].c+=flow;
}
ans+=dist[end];
flow_sum += flow;
}
//cout << flow_sum << endl;
return ans;
} int main()
{
//IN;
int n, m, x, y, z;
while(~scanf("%d%d", &n, &m)){
init();
int s = , t = * n;
for(int i = ; i < n; i++){
addedge(i, i + n, , );
}
addedge(, n + , , );
addedge(n, n << , , );
for(int i = ; i <= m; i++){
scanf("%d%d%d", &x, &y, &z);
addedge(x + n, y, , z);
}
int ans = MCMF(s, t);
printf("%d\n", ans);
}
return ;
}
UVALive - 6266 Admiral 费用流的更多相关文章
- uva 1658 Admiral - 费用流
vjudge传送门[here] 题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小. 正解是吧2 ...
- 【 UVALive - 2197】Paint the Roads(上下界费用流)
Description In a country there are n cities connected by m one way roads. You can paint any of these ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- UVALive - 7740 Coding Contest 2016 青岛区域赛 (费用流)
题意:每个点i有\(s_i\)个人和\(b_i\)份食物,每个人都要找到一份食物.现在有M条有向边,从点i到点j,容量为c,第一次走过不要紧,从第二次开始就要承担\(p(0<p<1)\)的 ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
随机推荐
- Linux bash常用快捷键
移动光标 ctrl-a 光标移动到行首 ctrl-e 光标移动到行尾 ctrl+xx 在行首和光标位置直接切换 ctrl-b 光标左移一位 ctrl-f 光标右移一位 alt-b 光标左移一词 alt ...
- nginx index.html
[root@web03 nginx]# ls -l|grep -v temptotal 36drwxr-xr-x 2 root root 4096 Jun 7 15:36 confdrwxr-xr-x ...
- Chrome扩展程序推荐
Chrome扩展程序 AdBlock 印象笔记 网页截图:注释&录屏 油猴 zenmate-vpn sourcegraph 推荐网站
- [terry笔记]11gR2_DataGuard搭建_拷贝数据文件
11gR2搭建dataguard环境: 自己做的实验,后续按照rman模式搭建.主备切换.模式调整等实验会陆续发上来. primary: OS:oel 6.4 database:11.2.0.4.0 ...
- Java设置Client Socket链接Server超时时间
Java设置Client Socket链接Server超时时间 学习了:http://blog.csdn.net/tterminator/article/details/52494141 http:/ ...
- Cocos2dx 小技巧(十五)话说ScrollView的delegate实现过程
附:本文參加了CSDN博客大赛.亲假设认为这篇文章不错,就大胆的来投上一票吧! !!http://vote.blog.csdn.net/Article/Details? articleid=34140 ...
- java webproject中logback换配置文件的路径
本人小站点: http://51kxd.com/ 欢迎大家不开心的时候訪问訪问,调节一下心情 web.xml中配置: <!-- windows logback.xml文件跟web容器(比 ...
- WET Dilutes Performance Bottlenecks
WET Dilutes Performance Bottlenecks Kirk Pepperdine THE IMPORTANCE OF THE DRY PRINCIPLE (Don't Repea ...
- node03--http
form.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- [Codeforces 757E] Bash Plays with Functions (数论)
题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...