HDU 3435 A new Graph Game 费用流
来源:互联网 发布:金和软件 编辑:程序博客网 时间:2024/05/16 10:44
//author: CHC//First Edit Time:2014-09-19 11:19//Last Edit Time:2014-09-19 11:25#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <vector>#include <map>#include <queue>#include <set>#include <algorithm>#include <limits.h>using namespace std;typedef long long LL;const int MAXN=1e+4;const int MAXM=1e+6;const int INF = INT_MAX;struct Edge{ int from,to,ci,cost,next; Edge(){} Edge(int _from,int _to,int _ci,int _cost,int _next):from(_from),to(_to),ci(_ci),cost(_cost),next(_next){}}e[MAXM];int head[MAXN],tot;int q[MAXM];int dis[MAXN],pre[MAXN],rec[MAXN],vis[MAXN];inline void init(){ memset(head,-1,sizeof(head)); tot=0;}inline void AddEdge1(int u,int v,int ci,int cost){ e[tot]=Edge(u,v,ci,cost,head[u]); head[u]=tot++; e[tot]=Edge(v,u,0,-cost,head[v]); head[v]=tot++;}inline bool spfa(int S,int T,LL &cost,LL &flow){ int i,h=0,t=0; for(i=0;i<=MAXN;i++){ dis[i]=INF; vis[i]=false; } q[h]=S; dis[S]=0; vis[S]=true; while(h<=t){ int u=q[h++]; vis[u]=false; for(i=head[u];~i;i=e[i].next){ int v=e[i].to; if(e[i].ci>0&&dis[v]>dis[u]+e[i].cost){ dis[v]=dis[u]+e[i].cost; pre[v]=u; rec[v]=i; if(!vis[v]){ vis[v]=1; q[++t]=v; } } } } if(dis[T]==INF)return false; int minn=INF; for(i=T;i!=S;i=pre[i]){ if(e[rec[i]].ci<minn) minn=e[rec[i]].ci; } for(i=T;i!=S;i=pre[i]){ //cost+=minn*e[rec[i]].cost; e[rec[i]].ci-=minn; e[rec[i]^1].ci+=minn; } cost+=dis[T]*minn; flow+=minn; return true;}inline void mincostmaxflow(int S,int T,LL &cost,LL &flow){ while(spfa(S,T,cost,flow));}int main(){ int n,m,t,cas=0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); init(); for(int i=0,x,y,v;i<m;i++){ scanf("%d%d%d",&x,&y,&v); AddEdge1(x<<1,(y<<1)|1,1,v); AddEdge1(y<<1,(x<<1)|1,1,v); } for(int i=1;i<=n;i++){ AddEdge1(0,(i<<1),1,0); AddEdge1((i<<1)|1,1,1,0); } LL cost=0,flow=0; mincostmaxflow(0,1,cost,flow); printf("Case %d: ",++cas); if(flow!=n) puts("NO"); else printf("%I64d\n",cost); } scanf(" "); return 0;}
0 0
- HDU A new Graph Game(费用流)
- hdu 3435 A new Graph Game(最小费用最大流)
- hdu 3435 A new Graph Game(最小费用流)
- HDU 3435 A new Graph Game(费用流)
- HDU 3435 A new Graph Game 费用流
- HDU 3435 A new Graph Game(费用流)
- hdu A new Graph Game (最小权匹配/费用流)
- HDU 3435 A new Graph Game(最小费用最大流)&HDU 3488
- HDU 3435A new Graph Game(网络流之最小费用流)
- HDU 3435 A new Graph Game //KM
- hdu 3435 A new Graph Game
- HDU-3435 A new Graph Game
- hdu 3435 A new Graph Game (KM)
- HDU 3435 A new Graph Game
- hdu 3435 A new Graph Game KM
- HDU 3435 A new Graph Game
- HDU 3435 A new Graph Game
- A new Graph Game HDU
- 那该死的DATALINK
- Oracle数据字典
- C++虚函数表解析
- JAVA学习第五十六课 — IO流(十)对象的序列化和反序列化 & RandomAccessFile
- 使用github上的开源框架SlidingMenu环境的搭建,以及getSupportActionBar方法不能找到的问题
- HDU 3435 A new Graph Game 费用流
- C#并行编程高级教程 20 个 C# 热点问题解答
- 在Android java代码中如何改变文件的权限
- 编译x264 出现No working C compiler found.
- 另一个例子
- POJ 2063 Investment(完全背包)
- cocos2d-x截屏功能clippingnode也能使用——白白
- IOS开发之──ASIHTTPRequest详解
- 机器人路径规划 转自《机器人控制系统的设计与matlab仿真》