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 题目大意:二分图匹配费用流.①最大匹配②最小原配变动 解题思路: 如果去掉第二个要求,那么就是裸 ...
随机推荐
- Error: Trying to open unclosed connection.
错误:试图打开未关闭的连接.在我自己写model文件的时候,重复打开了连接数据库的操作,所以报这种错误. 错误实例: 两个model文件: userModel.js var mongoose = re ...
- MVC下载文件方式 包括网络地址文件
MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){ string absol ...
- (wp8.1)样式资源
在开发wp的时候,难免要设置好多属性.但是有好多属性是重复的,我们可不可统一 设置样式呢,答案是肯定的 代码如下 <Page x:Class="Blue.MainPage" ...
- 如何检查linux 下是否安装java(jdk)环境
大家可通过下面五条命令来查看linux 系统是否安装了java 环境 1.java -version 2.which java 3.rpm -qa |grep java 4.echo $PATH 5. ...
- 评价PE基金绩效的常用指标
作为信息系统,辅助管理层决策是重要的功能之一.前文介绍了PE基金管理系统的建设,对PE业务的运转有了一些了解,但没有介绍如何评价PE基金的绩效,而这是管理层作出重大决策的主要依据之一.PE基金本质也是 ...
- ios中frame设置宽高计算的Demo
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ int totalHeightOfScrollView = scrollView.con ...
- SharePoint Online 缺少“将站点另存为模板”
之前文章行给出在SharePoint 2010 .SharePoint 2013 中将站点保存模板选项的文章.其实同样的问题出现在Microsoft Office 365的一部分SharePoint ...
- Python3+Selenium3+webdriver学习笔记6(多窗口切换处理)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...
- oracle 、server和my sql 语法区别
1.总结Oracle .sqlserver和mysql中查询10-20条记录的写法 一: oracle数据库写法: 1:select * from (select rownum rn ,* from ...
- window对象的几个重要方法
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...