题目地址:HDU 3435

这题刚上来一看,感觉毫无头绪。

。再细致想想。。

发现跟我做的前两道费用流的题是差点儿相同的。

能够往那上面转换。

建图基本差点儿相同。仅仅只是这里是无向图。建图依旧是拆点,推断入度出度。最后推断是否满流,满流的话这时的费用流是符合要求的。输出。不能满流的话,输出NO。

代码例如以下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[3000], source, sink, cnt, flow, cost, num;
int d[3000], vis[3000], pre[3000], cur[3000];
queue<int>q;
struct node
{
int u, v, cap, cost, next;
}edge[10000000];
void add(int u, int v, int cap, int cost)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].cost=-cost;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int spfa()
{
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
int minflow=INF, i;
q.push(source);
d[source]=0;
cur[source]=-1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
{
d[v]=d[u]+edge[i].cost;
minflow=min(minflow,edge[i].cap);
cur[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
if(d[sink]==INF) return 0;
flow+=minflow;
cost+=minflow*d[sink];
for(i=cur[sink];i!=-1;i=cur[edge[i^1].v])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
return 1;
}
void mcmf(int n)
{
while(spfa());
if(flow==n)
printf("%d\n",cost);
else
printf("NO\n");
}
int main()
{
int t, n, m, i, j, a, b, c;
scanf("%d",&t);
num=0;
while(t--)
{
num++;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
cnt=0;
source=0;
sink=2*n+1;
flow=0;
cost=0;
for(i=1;i<=n;i++)
{
add(source,i,1,0);
add(i+n,sink,1,0);
}
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b+n,1,c);
add(b,a+n,1,c);
}
printf("Case %d: ",num);
mcmf(n);
}
return 0;
}

HDU 3435A new Graph Game(网络流之最小费用流)的更多相关文章

  1. HDU 6321 Dynamic Graph Matching

    HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...

  2. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  3. HDU 3338 Kakuro Extension (网络流,最大流)

    HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...

  4. HDU 4280 Island Transport(网络流,最大流)

    HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...

  5. Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流)

    Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流) Description 10年一度的银河系赛车大赛又要开始了.作为全 ...

  6. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  7. HDU 1853Cyclic Tour(网络流之最小费用流)

    题目地址:pid=1853">HDU1853 费用流果然好奇妙. .还能够用来推断环...假设每一个点都是环的一部分并且每一个点仅仅能用到一次的话,那每一个点的初度入度都是1,这就能够 ...

  8. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  9. POJ 2195Going Home(网络流之最小费用流)

    题目地址:id=2195">POJ2195 本人职业生涯费用流第一发!!快邀请赛了.决定还是多学点东西.起码碰到简单的网络流要A掉.以后最大流费用流最小割就一块刷. 曾经费用流在我心目 ...

随机推荐

  1. 利用Python访问Mysql数据库

    首先要明确一点,我们在Python中需要通过第三方库才能访问Mysql. 有这样几种方式:Mysql-python(即MySQLdb).pymysql.mysql-connector.Mysql-py ...

  2. hdu6034[模拟] 2017多校1

    /*hdu6034[模拟] 2017多校1*/ //暴力模拟26个26进制数即可, 要注意进位 #include<bits/stdc++.h> using namespace std; t ...

  3. HDU1877 又一版 A+B

    Problem Description 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数.   Input 输入格式:测 ...

  4. (转)解决fasterxml中string字符串转对象json格式错误问题(无引号 单引号问题)

    原文地址:解决fasterxml中string字符串转对象json格式错误问题 com.fasterxml.jackson.databind.ObjectMapper mapper = new com ...

  5. Windows上安装DB2——从IBM官网得到90天试用版

    我在下面选的90天试用版: https://www.ibm.com/developerworks/cn/downloads/im/db2/ 进入下载页面,选择Windows https://www-0 ...

  6. spring-boot项目热部署以及spring-devtools导致同类不能转换

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  7. anaconda安装及环境变量配置

    最近无聊想的学习一下python. 首先可以在官网上下载与自己电脑兼容的anaconda安装包,网址如下https://www.continuum.io/downloads 下载好后就是一个exe文件 ...

  8. 【kindeditor】kindeditor的使用

    官网:http://kindeditor.net/demo.php 效果:

  9. ajax操作提交整个表单内容

    1 2 3 4 5 6 7 8 9 10 11 12 13 $.ajax({                 cache: true,                 type: "POST ...

  10. 使用Naive Bayes从个人广告中获取区域倾向

    RSS源介绍:https://zhidao.baidu.com/question/2051890587299176627.html http://www.rssboard.org/rss-profil ...