HDU 3435 KM A new Graph Game
和HDU 3488一样的,只不过要判断一下是否有解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, m; int W[maxn][maxn], lft[maxn];
int slack[maxn];
int Lx[maxn], Ly[maxn];
bool S[maxn], T[maxn]; bool match(int u)
{
S[u] = true;
for(int v = ; v <= n; v++) if(!T[v])
{
int t = Lx[u] + Ly[v] - W[u][v];
if( == t)
{
T[v] = true;
if(!lft[v] || match(lft[v]))
{
lft[v] = u;
return true;
}
}
else slack[v] = min(slack[v], t);
} return false;
} void update()
{
int a = INF;
for(int i = ; i <= n; i++) if(!T[i]) a = min(a, slack[i]);
for(int i = ; i <= n; i++)
{
if(S[i]) Lx[i] -= a; if(T[i]) Ly[i] += a;
else slack[i] -= a;
}
} void KM()
{
memset(Ly, , sizeof(Ly));
memset(lft, , sizeof(lft));
for(int i = ; i <= n; i++) Lx[i] = -INF;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) Lx[i] = max(Lx[i], W[i][j]); for(int i = ; i <= n; i++)
{
memset(slack, 0x3f, sizeof(slack));
for(;;)
{
memset(S, false, sizeof(S));
memset(T, false, sizeof(T));
if(match(i)) break;
update();
}
}
} int main()
{
int T; scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
W[i][j] = -INF;
for(int i = ; i < m; i++)
{
int u, v, d; scanf("%d%d%d", &u, &v, &d);
if(W[u][v] < -d) W[u][v] = W[v][u] = -d;
} KM(); bool ok = true;
int ans = ;
for(int i = ; i <= n; i++)
{
if(W[lft[i]][i] == -INF) { ok = false; break; }
ans += W[lft[i]][i];
}
printf("Case %d: ", kase);
if(!ok) puts("NO");
else printf("%d\n", -ans);
} return ;
}
代码君
HDU 3435 KM A new Graph Game的更多相关文章
- 【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 3435(KM算法最优匹配)
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5957 Query on a graph
HDU 5957 Query on a graph 2016ACM/ICPC亚洲区沈阳站 题意 \(N(N \le 10^5)\)个点,\(N\)条边的连通图. 有\(M \le 10^5\)操作: ...
- 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(最小费用流:有向环权值最小覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用 ...
- 【刷题】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 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- HDU 2853 (KM最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题目大意:二分图匹配费用流.①最大匹配②最小原配变动 解题思路: 如果去掉第二个要求,那么就是裸 ...
随机推荐
- Bootcamp Win10蓝牙鼠标的问题
运行services.msc找到Bluetooth support service 把启动类型从手动改为自动 重新连接蓝牙鼠标
- IQueryable和IEnumerable的区别
- VS连接Access数据库--连接字符串及执行查询语句的方法(增删改查,用户名查重,根据用户获取密码查询)
ACCESS数据的连接及语句执行操作,不难,久不用会生疏,每次都要找资料,干脆自己整理下,记录下来,需要的时候,直接查看,提高效率.也供初学者参考 1.连接字符串 public static stri ...
- VS2010/OpenGL配置
1.下载glut:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip 2.把解压得到的glut.h放到"C:\ ...
- I/O操做总结(二)
文件的操作 这一节我们来讨论关于文件自身的操作 不浪费唾沫了,用代码说话…… 实例1:创建文件对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- SpringMVC+Thymeleaf 简单使用
一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应用,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.浏览器解释 h ...
- UIButton 图片文字位置
在实际开发过程中经常在按钮上添加文字和图片,位置和图片的位置根据需求放置也是不一样的.下面实现了各种显示方式,如下图: UIButton+LSAdditions.h // // UIButton+LS ...
- pc端常见布局---垂直居中布局 单元素不定高
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 加载动画插件spin.js的使用随笔
背景: 在请求后台的“漫长”等待过程中,为了提升用户体验,需要一个类似 的加载动画效果,让用户明确现在处于请求过程中,而不是机子down掉或者网站死了 静态demo(未与后台交互): HTML代码如 ...
- 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm
题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...