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) ...
随机推荐
- IOS项目分层
上传者:踏浪帅 分类:其他(Others) 查看次数:408 下载次数:70 上传时间:2016-01-07 大小:3 KB 主项目中的分层主要包含四个模块,Main(主要).Expand(扩展).R ...
- org.apache.log4j日志级别
日志记录器(Logger)是日志处理的核心组件.log4j具有7种级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level)优先级从高到低:OFF.FATAL ...
- oracle缩写与术语
术语 定义活动会话池 资源组或子计划允许的当前活动会话数ADDM Automatic Database Diagnostic Monitor(自动数据库诊断监视程序)ASM Automatic Sto ...
- 向Docx4j生成的word文档添加图片和布局--第一部分
原文标题:Adding images and layout to your Docx4j-generated word documents, part 1 原文链接:http://blog.iprof ...
- 强制关机导致ORA-03113
数据库启动报错:无法打开数据库. [oracle@localhost ORCL]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Productio ...
- BBS - 预备知识
一.中介模型 四个项目: 苑昊 博客(BBS) (7-8) CRM 1.权限组件 (3) 2.start组件 -- admin (5) 1.使用 2.源码 django 源码 (面向对象) 以源码为导 ...
- 七夕节---hdu1215(打表求因子和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1215 给你一个数n(1<=n<=50w)求n的所有因子和, 由于n的范围比较大,所以要采用 ...
- python sort、sorted高级排序技巧(转)
add by zhj: 没找到原文.可以按多个维度进行排序,而且可以指定他们的排序方向,如果维度都是数字,排序比较容易,用+/-号就可以 指定排序方向.否则,就调用多次sorted进行排序了,而且要按 ...
- 23.如何查看一个keystore的具体签名信息
keytool -list -v -keystore zhangzu.keystore
- Openstack(七)keystone
官方安装文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/index.html 7.1 keystone简介 Keystone 中 ...