hdu 6201(最小费用最大流)
transaction transaction transaction
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1003 Accepted Submission(s):
488

- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<cstdlib>
- #include<string.h>
- #include<set>
- #include<vector>
- #include<queue>
- #include<stack>
- #include<map>
- #include<cmath>
- typedef long long ll;
- typedef unsigned long long LL;
- using namespace std;
- const double PI=acos(-1.0);
- const double eps=0.0000000001;
- const int INF=0x3f3f3f3f;
- const int N=+;
- int a[N];
- int head[N];
- int dis[N];
- int pre[N];
- int vis[N];
- int tot;
- int m,n;
- struct node{
- int from,to,next,flow,cost;
- }edge[N<<];
- void init(){
- memset(head,-,sizeof(head));
- tot=;
- }
- void add(int u,int v,int c,int cost){
- edge[tot].from=u;
- edge[tot].to=v;
- edge[tot].flow=c;
- edge[tot].cost=cost;
- edge[tot].next=head[u];
- head[u]=tot++;
- edge[tot].from=v;
- edge[tot].to=u;
- edge[tot].flow=;
- edge[tot].cost=-cost;
- edge[tot].next=head[v];
- head[v]=tot++;
- }
- int spfa(int s,int t){
- memset(pre,-,sizeof(pre));
- memset(dis,INF,sizeof(dis));
- memset(vis,,sizeof(vis));
- queue<int>q;
- dis[s]=;
- vis[s]=;
- q.push(s);
- while(!q.empty()){
- int x=q.front();
- q.pop();
- vis[x]=;
- for(int i=head[x];i!=-;i=edge[i].next){
- int v=edge[i].to;
- if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
- dis[v]=edge[i].cost+dis[x];
- pre[v]=i;
- if(vis[v]==){
- vis[v]=;
- q.push(v);
- }
- }
- }
- }
- if(pre[t]==-)return ;
- return ;
- }
- int MCMF(int s,int t){
- int flow=;
- int cost=;
- while(spfa(s,t)){
- int minn=INF;
- for(int i=pre[t];i!=-;i=pre[edge[i].from]){
- minn=min(minn,edge[i].flow);
- }
- for(int i=pre[t];i!=-;i=pre[edge[i].from]){
- edge[i].flow=edge[i].flow-minn;
- edge[i^].flow=edge[i^].flow+minn;
- cost=edge[i].cost+cost;
- // cout<<cost<<endl;
- }
- flow=flow+minn;
- }
- return cost;
- }
- int main(){
- int tt;
- scanf("%d",&tt);
- while(tt--){
- init();
- scanf("%d",&n);
- for(int i=;i<=n;i++){
- scanf("%d",&a[i]);
- }
- int s=;
- int s1=n+;
- int t=n+;
- add(s,s1,,);
- for(int i=;i<=n;i++){
- add(s1,i,,-a[i]);
- }
- for(int i=;i<=n;i++){
- add(i,t,,a[i]);
- }
- for(int i=;i<=n-;i++){
- int u,v,cost;
- scanf("%d%d%d",&u,&v,&cost);
- add(u,v,,cost);
- add(v,u,,cost);
- }
- cout<<abs(MCMF(s,t))<<endl;
- }
- }
hdu 6201(最小费用最大流)的更多相关文章
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1533(最小费用最大流)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 4862KM&最小费用最大流
/*最小K路径覆盖的模型,用费用流或者KM算法解决, 构造二部图,X部有N*M个节点,源点向X部每个节点连一条边, 流量1,费用0,Y部有N*M个节点,每个节点向汇点连一条边,流量1, 费用0,如果X ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- hdu 3667(最小费用最大流+拆边)
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 6437 /// 最小费用最大流 负花费 SPFA模板
题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ...
- hdu 4067(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...
- hdu 2485(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
随机推荐
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- 用Docker构建Tomcat镜像
构建tomcat镜像 创建工作目录 [root@elk-node2 tomcat]# mkdir tomcat [root@elk-node2 tomcat]# cd tomcat [root@elk ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- HDU4496 D-City【基础并查集】
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- reading/writing files in Python
file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...
- led1,1s取反,led2计数10次取反
1 //利用定时器0 1s,led1取反,利用计数器1,跳10,取反 #include<reg52.h> #define uchar unsigned char #define uint ...
- 基于XML文档的声明式事务配置
<!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.t ...
- 【00】angular学习网站
[00] 学习资料: http://angularjs.cn/ 英文API:http://docs.angularjs.cn/api 中文API;http://www.apjs.n ...
- Ural 1114 Boxes
Boxes Time Limit: 600ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 1114 ...