hdu 3435 A new Graph Game
2014-03-20 21:32
316 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3435
View Code
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algorithm> #define inf 0x3f3f3f3f #define maxn 54444 using namespace std; queue<int>q; struct node { int u,v,next,val,f; } p[maxn]; int head[maxn]; bool vis[maxn]; int dis[maxn]; int pre[maxn]; int n,m,s,t,e; void add(int u,int v,int f,int c) { p[e].u=u; p[e].v=v; p[e].f=f; p[e].val=c; p[e].next=head[u]; head[u]=e++; p[e].u=v; p[e].v=u; p[e].f=0; p[e].val=-c; p[e].next=head[v]; head[v]=e++; } bool spfa() { int j,k,i; while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); memset(pre,-1,sizeof(pre)); vis[s]=true; dis[s]=0; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(j=head[u]; j!=-1; j=p[j].next) { int v=p[j].v; if(p[j].f&&dis[u]+p[j].val<dis[v]) { dis[v]=dis[u]+p[j].val; pre[v]=j; if(!vis[v]) { vis[v]=true; q.push(v); } } } } return dis[t]!=inf; } int mincost() { int ret=0,u; while(spfa()) { u=pre[t]; ret+=dis[t]; while(u!=-1) { p[u].f--; p[u^1].f++; u=pre[p[u].u]; } } return ret; } int main() { int case1; scanf("%d",&case1); for(int k=1; k<=case1; k++) { e=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); s=0; t=2*n+1; for(int i=1; i<=m; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b+n,1,c); add(b,a+n,1,c); } for(int i=1; i<=n; i++) { add(s,i,1,0); add(i+n,t,1,0); } int j; int ans=mincost(); for(j=head[s]; j!=-1; j=p[j].next) if(p[j].f!=0) break; if(j==-1) printf("Case %d: %d\n",k,ans); else printf("Case %d: NO\n",k); } return 0; }
View Code
相关文章推荐
- HDU 3435 A new Graph Game
- hdu 3435 A new Graph Game KM
- 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(二分图最优匹配:有向环覆盖)
- HDU 3435 A new Graph Game
- HDU 3435 A new Graph Game(KM完美匹配)
- HDU 3435 A new Graph Game 费用流
- hdu 1853 Cyclic Tour && 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(费用流)
- 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
- A new Graph Game HDU - 3435 (二分匹配思想)(最小花费最大流)
- HDU 3435 A new Graph Game(最小费用最大流)&HDU 3488