HDU 3488
http://acm.hdu.edu.cn/showproblem.php?pid=3488
原来写过的一道题,今天重新看费用流又做了一遍
题意:给一个图,求环的并(权值和最小)
思路:每个点只能走一次,且都要走,所以一个点的出度入度均为1,因此拆点建图跑二分图最优匹配
用费用流写的,速度比km慢
#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 t ;
scanf("%d",&t) ;
while(t--)
{
cnt= ;
memset(head,-,sizeof(head)) ;
scanf("%d%d",&n,&m) ;
int ss= ;
int st=*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(,i,,) ;
for(int i=n+ ;i<=*n ;i++)
add(i,st,,) ;
int ans=MCMF(ss,st,st+) ;
printf("%d\n",ans) ;
}
return ;
}
HDU 3488的更多相关文章
- Hdu 3488 Tour (KM 有向环覆盖)
题目链接: Hdu 3488 Tour 题目描述: 有n个节点,m条有权单向路,要求用一个或者多个环覆盖所有的节点.每个节点只能出现在一个环中,每个环中至少有两个节点.问最小边权花费为多少? 解题思路 ...
- hdu 3488 Tour
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都只 ...
- Tour HDU - 3488 有向环最小权值覆盖 费用流
http://acm.hdu.edu.cn/showproblem.php?pid=3488 给一个无源汇的,带有边权的有向图 让你找出一个最小的哈密顿回路 可以用KM算法写,但是费用流也行 思路 1 ...
- HDU 3488 Tour(最小费用流:有向环最小权值覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- 图论(二分图,KM算法):HDU 3488 Tour
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- Q - Tour - hdu 3488(最小匹配值)
题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...
- HDU 3488 Tour (最大权完美匹配)【KM算法】
<题目链接> 题目大意:给出n个点m条单向边边以及经过每条边的费用,让你求出走过一个哈密顿环(除起点外,每个点只能走一次)的最小费用.题目保证至少存在一个环满足条件. 解题分析: 因为要求 ...
- Tour HDU - 3488(最大权值匹配)
Tour In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one- ...
随机推荐
- Spring中bean作用域属性scope
关键字: spring中属性scope的prototype是什么意思 默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spri ...
- 关于iBatis配置xml文件时出现中文注释出错的一个问题(很坑爹.)
才开始我没有使用SqlMap.properties来配置连接信息.所以直接用ctrl + shift + C然后往里面添加的中文注释 例: <!-- 注释--> 运行的时候报: Error ...
- 机器学习 MLIA学习笔记(二)之 KNN算法(一)原理入门实例
KNN=K-Nearest Neighbour 原理:我们取前K个相似的数据(排序过的)中概率最大的种类,作为预测的种类.通常,K不会大于20. 下边是一个简单的实例,具体的含义在注释中: impor ...
- Redis<六> Key通用操作
1). KEYS pattern : 查找所有符合给定模式 pattern 的 key . 如 keys * , keys *list* 2). DEL key [key ...] : 删除给定的一个 ...
- Java基础九--抽象类
Java基础九--抽象类 一.抽象类介绍 /*抽象类:抽象:笼统,模糊,看不懂!不具体. 特点:1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中 ...
- [.NET开发] NPOI导出
//导出全部 expertPara = GetExpetPara(); expertPara.BeginIndex = pager.CurrentPageIndex; expertPara.EndIn ...
- spoj Fast Multiplication
题意:乘法 要用nlogn的fft乘法. //#pragma comment(linker,"/STACK:1024000000,1024000000") #include< ...
- 2018焦作网络赛Give Candies
一开始忽略了欧拉定理指数部分是modphi(n-1)没有memset,减法后面没加0:
- JS-构造函数2
一.如何创建对象 1.对象字面量 var obj1={ name:"吻别", singer:"张学友", type:"流行" } 2.构造函 ...
- Krapo 2
The krpano Viewer is a small and very flexible high-performance viewer for all kind of panoramic ima ...