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 ...
随机推荐
- 一:Java之面向对象基本概念
1.面向对象 面向对象(Object Oriented)是一种新兴的程序设计方法,或者是一种新的程序设计规范(paradigm).其基本思想是使用对象.类.继承.封装.多态等基本概念来进行程序设计.从 ...
- MyEclipse常见错误汇总,中英注释版(长期更新)
No.1 当一条语句漏写分号时错误描述如下 Syntax error, insert ";" to complete Statement(语法错误:插入分号完成语句描述) No.2 ...
- CSDN博客2014年4月24日清理缓存
亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...
- node05---模块
Node.js开发服务器,数据.路由.本地关心的效果,交互: Node.js实际上是极客开发出的一个小玩具,不是银弹.有着别人不具备的怪异特点: 首先,Node不为每个用户开辟一个线程,所以非常单线程 ...
- C# Hook
C# Hook原理及EasyHook简易教程 前言 在说C# Hook之前,我们先来说说什么是Hook技术.相信大家都接触过外挂,不管是修改游戏客户端的也好,盗取密码的也罢,它们都是如何实现的呢? 实 ...
- reverse(两种反向生成url django原生形式和rest_framework中版本的形式)
reverse(两种反向生成url django原生形式和rest_framework中版本的形式) views.py from django.shortcuts import render,Http ...
- [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...
- java9新特性-13-增强的 Stream API
1.使用说明 Java 的 Steam API 是java标准库最好的改进之一,让开发者能够快速运算,从而能够有效的利用数据并行计算.Java 8 提供的 Steam 能够利用多核架构实现声明式的数据 ...
- nodejs 通过 get获取数据修改redis数据
如下代码是没有报错的正确代码 我通过https获取到数据 想用redis set一个键值存储 现在我掉入了回调陷阱res.on 里面接收到的数据是data 里面如果放入 client.on('conn ...
- SpringMVC学习一:SpringMVC的配置
SpringMVC的配置主要分为两部分: 1.xml文件配置 2.注解的配置 SpringMVC配置的步骤如下: 1.在将SpringMVC的jar包导入到web项目中后,先配置web.xml 文件. ...