http://acm.hdu.edu.cn/showproblem.php?pid=3435

同下题,只是这题是双向边,同时让我认识到了一个问题,一个图拆点做二分图完美匹配的本质是求该图环的并

http://www.cnblogs.com/xiaohongmao/p/3873957.html

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std ;
const int INF=0xfffffff ;
struct node{
int s,t,cap,cost,nxt ;
}e[] ;
int sumflow ;
int n,m,cnt,head[],vis[],dis[],pre[] ;
void add(int s,int t,int cap,int cost)
{
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].cost=cost ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap= ;e[cnt].cost=-cost ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
}
int spfa(int s,int t,int N)
{
for(int i= ;i<=N ;i++)
dis[i]=INF ;
dis[s]= ;
memset(vis,,sizeof(vis)) ;
memset(pre,-,sizeof(pre)) ;
vis[s]= ;
queue <int> q ;
q.push(s) ;
while(!q.empty())
{
int u=q.front() ;
q.pop() ;
vis[u]= ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(e[i].cap && dis[tt]>dis[u]+e[i].cost)
{
dis[tt]=dis[u]+e[i].cost ;
pre[tt]=i ;
if(!vis[tt])
{
vis[tt]= ;
q.push(tt) ;
}
}
}
}
if(dis[t]==INF)return ;
return ;
}
int MCMF(int s,int t,int N)
{
int flow,minflow,mincost ;
mincost=flow= ;
while(spfa(s,t,N))
{
minflow=INF ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
minflow=min(minflow,e[i].cap) ;
flow+=minflow ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
{
e[i].cap-=minflow ;
e[i^].cap+=minflow ;
}
mincost+=dis[t]*minflow ;
}
sumflow=flow ;//最大流
return mincost ;
}
int main()
{
int CAS ;
scanf("%d",&CAS) ;
for(int cas= ;cas<=CAS ;cas++)
{
cnt= ;
memset(head,-,sizeof(head)) ;
scanf("%d%d",&n,&m) ;
int S= ;
int T=*n+ ;
for(int i= ;i<m ;i++)
{
int s,t,v ;
scanf("%d%d%d",&s,&t,&v) ;
add(s,t+n,,v) ;
add(t,s+n,,v) ;
}
for(int i= ;i<=n ;i++)
add(S,i,,) ;
for(int i=n+ ;i<=*n ;i++)
add(i,T,,) ;
int ans=MCMF(S,T,T+) ;
printf("Case %d: ",cas) ;
if(sumflow!=n)puts("NO") ;
else printf("%d\n",ans) ;
}
return ;
}

HDU 3455的更多相关文章

  1. HDU 3455 Leap Frog(线性DP)

    Problem Description Jack and Jill play a game called "Leap Frog" in which they alternate t ...

  2. HDU 3455 Leap Frog 2016-09-12 16:34 43人阅读 评论(0) 收藏

    Leap Frog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. Linux——vim/vi 简单学习笔记

    Vim/Vi是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件.Vim/Vi 没有菜单,只有命令. 早前也用过Vim变过C++/C的代码, ...

  2. testNG 学习笔记 Day2 配置testNG自带的监听器

    IntelliJ IDEA配置testNG自带的监听器的时候,操作如下菜单栏中 run ----> 下拉菜单中的 Edit Configurations ----> 新矿口中TeatNG下 ...

  3. Jsp基础语法(由简入杂)

    JSP基础语法 一,JSP简介 Jsp是一个简化的Servlet设计,是在服务器端执行,他实现了再Java中使用HTML标签. Jsp是一种动态网页技术标准也是JAVAEE的标准 二,常见动态网站开发 ...

  4. Flutter学习笔记(一)

    记得flutter出来的时候,官方推荐的是使用IntelliJ IDEA,当时个人尝试了一下,比较麻烦,整个过程比较漫长. 进入2018年,再去看的时候,官方推荐使用Android Studio和VS ...

  5. vscode python3 配置生成任务

    一直用sublime,但是ubuntu下输入中文有问题,解决起来太麻烦: pycharm太重.虚拟机一开+Chrome打开10几个页面,然后再运行pycharm,静音轻薄笔记本CPU和8G内存基本都占 ...

  6. URAL 1741 Communication Fiend

    URAL 1741 思路: dp 状态:dp[i][1]表示到第i个版本为正版的最少流量花费 dp[i][0]表示到第i个版本为盗版的最少流量花费 初始状态:dp[1][0]=dp[0][0]=0 目 ...

  7. Lua 中与字符串有关的函数学习

    string1 = "lua" print(string.upper(string1)) string2 = 'LGS' print(string.lower(string2)) ...

  8. 动态规划-Largest Sum of Averages

    2018-07-12 23:21:53 问题描述: 问题求解: dp[i][j] : 以ai结尾的分j个部分得到的最大值 dp[i][j] = max{dp[k][j - 1] + (ak+1 + . ...

  9. php 获取自己的公网IP

    <?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP ...

  10. English trip -- Phonics 4 元音字母 i

    IPA   国际音标(英语:International Phonetic Alphabet [ælfəbet],缩写:IPA),早期又称万国音标 VOWELS [ɪ]   [u]   [ʌ]   [ɒ ...