题目地址: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. verilog写的LCD1602 显示

    在读本文之前,请先阅读 LCD1602 的 datasheet(百度到处都是) ,熟悉有关的11条指令集. LCD1602的11个指令集链接 http://www.cnblogs.com/aslmer ...

  2. TensorFlow——交互式使用会话:InteractiveSession类

    目的是在交互式环境下(如jupyter),手动设定当前会话为默认会话,从而省去每次都要显示地说明sess的繁琐,如:Tensor.ecal(session=sess)或sess.Operation.r ...

  3. Apache Log4j 2 is Coming

    刚刚从同事那里得知,log4j 2 出beta版本了. 有啥提升呢? Improved PerformanceLog4j 2 contains next-generation Asynchronous ...

  4. Vue样式绑定、事件绑定

    1.样式绑定 1.1class类标签绑定 <p :class="对象"> <p :class="数组"> <p :class=&q ...

  5. jQuery全屏滚动插件fullPage.js使用中遇到的问题(滑动轮播效果无效)

    1.fullPage.js是什么? fullPage.js 是一个基于 jQuery ,用来制作全屏网站的插件. 2.兼容性: - jquery兼容:兼容 jQuery 1.7+ - 浏览器兼容: I ...

  6. HDU——2093考试排名(string类及其函数的运用以及istringstream)

    考试排名 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. HDU——1042N!(大数阶乘乘法)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  8. linux查找文件命令

    (2)find /etc -name httpd.conf #在/etc目录下文件httpd.conf

  9. 【CF652C】Foe Pairs(线性扫描)

    题意:给你1-n的一个排列和m组数对,问有多少区间不包含任意一个数对. (1 ≤ n, m ≤ 3·105) 思路:数据范围过大,不能用容斥原理 f[i]表示以位置i上的数为左端点,右端点最小到哪里 ...

  10. 现象: shell脚本中source aaa.sh时提示 source: not found

    原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash来进行解析的. 改回方法: 命令行执行:sudo dpkg-reconfigure dash 在界 ...