HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3435
题意:
有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小。
思路:
费用流,注意判断重边,否则会超时。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
typedef long long LL; const int maxn=+;
const int INF=0x3f3f3f3f; int map[maxn][maxn]; struct Edge
{
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
}; struct MCMF
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n)
{
this->n = n;
for (int i = ; i<n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back(Edge(from, to, cap, , cost));
edges.push_back(Edge(to, from, , , -cost));
m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool BellmanFord(int s, int t, int &flow, LL & cost)
{
for (int i = ; i<n; i++) d[i] = INF;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF; queue<int> Q;
Q.push(s);
while (!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i<G[u].size(); i++){
Edge& e = edges[G[u][i]];
if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += (LL)d[t] * (LL)a[t];
for (int u = t; u != s; u = edges[p[u]].from){
edges[p[u]].flow += a[t];
edges[p[u] ^ ].flow -= a[t]; }
return true;
} int MincostMaxdflow(int s, int t, LL & cost)
{
int flow = ; cost = ;
while (BellmanFord(s, t, flow, cost) );
return flow;
}
}t; int n,m; int main()
{
//freopen("D:\\input.txt", "r", stdin);
int T;
int kase=;
scanf("%d",&T);
int u,v,d;
while(T--)
{
memset(map,,sizeof(map));
scanf("%d%d",&n,&m);
int src=,dst=*n+;
t.init(dst+);
for(int i=;i<=n;i++)
{
t.AddEdge(src,i,,);
t.AddEdge(i+n,dst,,);
}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&d);
if(map[u][v]== || map[u][v]>d)
{
t.AddEdge(u,v+n,,d);
t.AddEdge(v,u+n,,d);
map[u][v]=map[v][u]=d;
}
}
long long cost;
int flow=t.MincostMaxdflow(src,dst,cost);
printf("Case %d: ",++kase);
if(flow==n) printf("%d\n",cost);
else printf("NO\n");
}
return ;
}
HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)的更多相关文章
- hdu 3435 A new Graph Game
http://acm.hdu.edu.cn/showproblem.php?pid=3435 #include <cstdio> #include <iostream> #in ...
- 【刷题】HDU 3435 A new Graph Game
Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...
- HDU 3435 A new Graph Game(最小费用最大流)&HDU 3488
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【HDU 3435】 A new Graph Game (KM|费用流)
A new Graph Game Problem Description An undirected graph is a graph in which the nodes are connected ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- HDU 3488 Tour(最小费用流:有向环最小权值覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3488 题意: 给出n个点和m条边,每条边有距离,把这n个点分成1个或多个环,且每个点只能在一个环中,保证有解. ...
- hdu 3435(KM算法最优匹配)
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- elk日志分析与发掘深入分析
elk日志分析与挖掘深入分析 1 为什么要做日志采集? 2 挖财自己的日志采集和分析体系应该怎么建? 2.1 日志的采集 2.2 日志的汇总与过滤 2.3 日志的存储 2.4 日志的分析与查询 3 需 ...
- .net asp 在1.asp页面嵌入另一个页面2.asp
<iframe src="http://www.baidu.com" width="100%" height="100%" onloa ...
- 微信小程序 --- 获取当前坐标
获取位置:get.location type:wgs84(是全球定位系统,获取的坐标,gcj02是国家测绘局给出的坐标) btnclick:function(){ wx.getLocation({ t ...
- 在线预览文档(支持word、excel、ppt、pdf)+在线预览文档html版(转)
1.首先上网搜索一下有什么解决方案 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf (1) a. ...
- MySQL监控、性能分析——工具篇(转载)
MySQL越来越被更多企业接受,随着企业发展,MySQL存储数据日益膨胀,MySQL的性能分析.监控预警.容量扩展议题越来越多.“工欲善其事,必先利其器”,那么我们如何在进行MySQL性能分析.监控预 ...
- string unicode utf8 ascii in python and js
http://www.jb51.net/article/62155.htm http://www.cnblogs.com/dkblog/archive/2011/03/02/1980644.html ...
- SELINUX导致数据修改权限不成功
SELINUX导致数据修改权限不成功基本概念参考:https://blog.csdn.net/yanjun821126/article/details/80828908 查看SELinux状态: ./ ...
- 开发一个根据xml创建代理类的小框架
github地址 https://github.com/1367356/GradleTestUseSubModule/tree/master/CreateMyFrameWork 1:定义一些规则
- tar 压缩解压命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- luarocks模块管理工具
1.简介 该软件包可以安装和更新lua的第三方模块. 2.下载地址 请在 http://luarocks.org/releases/ 页面选择需要的软件包. wget http://luarocks. ...