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

和下题一模一样,求一个图环的并,此题的题干说的非常之裸露

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()
{
while(~scanf("%d%d",&n,&m))
{
cnt= ;
memset(head,-,sizeof(head)) ;
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) ;
}
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+) ;
if(sumflow!=n)puts("-1") ;
else printf("%d\n",ans) ;
}
return ;
}

HDU 1853的更多相关文章

  1. HDU(1853),最小权匹配,KM

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Other ...

  2. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  3. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

  4. hdu 1853 最小费用流好题 环的问题

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  5. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  6. HDU 1853 Cyclic Tour[有向环最小权值覆盖]

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  7. HDU 1853 Cyclic Tour(最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  8. 【刷题】HDU 1853 Cyclic Tour

    Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...

  9. hdu 1853(拆点判环+费用流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

随机推荐

  1. Linux——GRUB简单学习笔记

    GRUB的配置文件默认为  :/boot/grub/grub.conf # ls -l /etc/grub.conf GRUB配置选项: default定义缺省启动系统 timeout定义缺省等待时间 ...

  2. Cocos2d-x学习笔记(二)AppDelegate类详解

    由源代码,可得到如下的类继承关系: 1. 在方法applicationDidFinishLaunching中,首先会调用CCDirector* pDirector = CCDirector::shar ...

  3. 转载:oracle 11g ADG实施手册(亲测,已成功部署多次)

    https://www.cnblogs.com/yhfssp/p/7815078.html 一:实验环境介绍 虚拟机系统: RHEL Linux 6.4(64位) 数据库版本: Oracle 11gR ...

  4. docker相关杂项

    代理 在vscode里build image习惯了,但是今天 从docker hub上pull python镜像,最后一个层,始终是waiting状态,pull不下来 好像不能临时.只能设置 http ...

  5. centos7: vsftpd安装及启动

    安装: yum -y install vsftpd service vsftpd start  注意这句:centos7不能这么启动了 chkconfig vsftpd on vsftpd.conf配 ...

  6. English trip -- VC(情景课)2 A At school

    xu言: Sometimes, I feel very confused. However, there will always be a weak light in a corner to ligh ...

  7. Mac安装软件时 提示已损坏的解决方法

    进入终端: sudo spctl --master-disable

  8. 关于一致性hash详细

    一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...

  9. GitHub出现Permissiondenied (publickey).

    cd ~/.ssh ssh -T git@github.com 出现Permission denied(publickey).意思表示缺少公钥. ssh-keygen -t rsa -C " ...

  10. robot framework学习笔记1之_环境安装(win7)

    一.简介 Robotframework是基于Python的自动化测试框架.使用关键字驱动的测试方法,自带丰富的库函数可直接引用,可使用Java/Python进行功能库扩展,测试用例使用TSV/HTML ...