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. 纯CSS实现一个微信logo,需要几个标签?

    博客已迁移至http://lwzhang.github.io. 纯CSS实现一个微信logo并不难,难的是怎样用最少的html标签实现.我一直在想怎样用一个标签就能实现,最后还是没想出来,就只好用两个 ...

  2. hdu 5586 Sum 基础dp

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Desc ...

  3. An error occurred during installation: No such plugin: cloudbees-folder

    解决办法:下载cloudbees-folder.hpi放在目录/usr/local/tomcat/webapps/jenkins/WEB-INF/detached-plugins/下,重启tomcat ...

  4. angular5 ng-bootstrap和ngx-bootstrap区别

    https://angular.cn/resources ngx-bootstrap 安装: npm install ngx-bootstrap --save 再引入css <link href ...

  5. Redis分布式锁实现简单秒杀功能

    这版秒杀只是解决瞬间访问过高服务器压力过大,请求速度变慢,大大消耗服务器性能的问题. 主要就是在高并发秒杀的场景下,很多人访问时并没有拿到锁,所以直接跳过了.这样就处理了多线程并发问题的同时也保证了服 ...

  6. 雷林鹏分享:Ruby 数据类型

    Ruby 数据类型 本章节我们将为大家介绍 Ruby 的基本数据类型. Ruby支持的数据类型包括基本的Number.String.Ranges.Symbols,以及true.false和nil这几个 ...

  7. Confluence 6 使用 LDAP 授权连接一个内部目录 - 高级设置

    ted Groups 为嵌套组启用或禁用支持. 一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上级用户组的权限,是系统的权限配置变 ...

  8. 关于二级指针的使用(使用node指针建树)

    struct node { int v; node *l,*r; }*p; 使用二级指针建树的话,如果p是非全局变量且一开始没有指向变量的话递归建树时必然要传递参数,但是如果只是简单的build(no ...

  9. UVA-1252 Twenty Questions (状压DP)

    题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问? 题目分析:定义dp(s,a ...

  10. Get和Load的区别----hibernate

    Get和Load的区别