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 题目大意:二分图匹配费用流.①最大匹配②最小原配变动 解题思路: 如果去掉第二个要求,那么就是裸 ...
随机推荐
- C#基础文件file的各种套路
File的各种套路 //创建一个文件 //File.Create(@"C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteL ...
- Java获取服务器系统默认编码格式
大佬教的,做个笔记方法一(推荐):新建一个jsp页面在webapp下然后添加 <% out.print(System.getProperties().getProperty("file ...
- 有关在python中使用Redis(一)
python作为一种处理数据的脚本语言本身有许多方法函数供大家使用,有时候为了提升数据处理速度(如海量数据的访问或者海量数据的读取),涉及分布式管理架构,可能需要用到Redis,Redis是一个开源的 ...
- 1068 乌龟棋 2010年NOIP全国联赛提高组
1068 乌龟棋 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- 利用任务计划自动删除指定日期的SQLServer备份文件
利用任务计划自动删除指定日期的SQLServer备份文件 命令FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D ...
- Python+selenium之下载文件
一.Firefox文件下载 Web容许我们设置默认的文件下载路劲,文件会自动下载并且存放在指定的目录下. from selenium import webdriver import os fp = w ...
- Html style="visibility:hidden"与style="display:none"的区别
style="visibility:hidden": 使对象在网页上隐藏,但该对象在网页上所占的空间没有改变. style="display:none": 使对 ...
- CSS中padding、margin两个重要属性的详细介绍及举例说明
http://www.x6x8.com/IT/199.html 本文将讲述HTML和CSS的关键—盒子模型(Box model). 理解Box model的关键便是margin和padding属性, ...
- jsp之获传统方式取后台数据
1.建立模型对象: package com.java.model; public class Student { private String name; private int age; publi ...
- HDU 5500 Reorder the Books (水题)
题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...