HDU-3435 A new Graph Game

继续是最小权值环覆盖。

注意的是边为双向。

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 1234
#define M 23456
#define MAX 1<<30
#define ll long long
using namespace std;
int read()
{
	int x=0, f=1; char ch=getchar();
	while (!isdigit(ch)) { if (ch==‘-‘) f=-1; ch=getchar(); }
	while (isdigit(ch)) { x=x*10+ch-‘0‘; ch=getchar(); }
	return x*f;
}

struct edge{int x, y, z, n;} e[M]; int fir[N], en;
int n, m, l[N], st[N], lx[N], ly[N];
bool vx[N], vy[N];

inline void Add(int x, int y, int z)
{
	en++, e[en]=(edge){x, y, z, fir[x]}, fir[x]=en;
	en++, e[en]=(edge){y, x, z, fir[y]}, fir[y]=en;
}

bool Find(int x)
{
	vx[x]=1;
	for(int o=fir[x], y=e[o].y; o; o=e[o].n, y=e[o].y)
	{
		if (vy[y]) continue;
		int a=lx[x]+ly[y]-e[o].z;
		if (!a)
		{
			vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return 1; }
		}
		else st[y]=min(st[y], a);
	}
	return false;
}

inline int km()
{
	clr(ly, 0); clr(l, 0); rep(i, 1, n) lx[i]=-MAX;
	rep(i, 1, en) if (lx[e[i].x]<e[i].z) lx[e[i].x]=e[i].z;
	rep(i, 1, n) if (lx[i]==-MAX) return 1;
	rep(i, 1, n)
	{
		rep(j, 1, n) st[j]=MAX;
		while (1)
		{
			clr(vx, 0); clr(vy, 0);
			if (Find(i)) break; int a=MAX;
			rep(j, 1, n) if (!vy[j] && st[j]<a) a=st[j]; if (a==MAX) break;
			rep(j, 1, n) if (vx[j]) lx[j]-=a;
			rep(j, 1, n) if (vy[j]) ly[j]+=a; else st[j]-=a;
		}
	}
	int a=0;
	rep(i, 1, n) if (!l[i]) return 1; else a+=lx[i]+ly[i];
	return a;
}

int main()
{
	int t=read(), tt=0; while (tt++<t)
	{
		n=read(), m=read(); en=0; clr(fir, 0);
		rep(i, 1, m) { int x=read(), y=read(), z=read(); Add(x, y, -z); }
		int ans=-km();
		if (ans!=-1) printf("Case %d: %d\n", tt, ans); else printf("Case %d: NO\n", tt);
	}
	return 0;
}

  

时间: 2024-12-28 12:06:32

HDU-3435 A new Graph Game的相关文章

hdu 1853 Cyclic Tour &amp;&amp; hdu 3435 A new Graph Game(简单KM算法)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1478    Accepted Submission(s): 750 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用流,注意判断重边,否则会超时. 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<queue> 6 using names

HDU 3435 A new Graph Game(最小费用最大流)&amp;amp;HDU 3488

A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1849    Accepted Submission(s): 802 Problem Description An undirected graph is a graph in which the nodes are connected by undir

HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488

A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1849    Accepted Submission(s): 802 Problem Description An undirected graph is a graph in which the nodes are connected by undir

【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 by undirected arcs. An undirected arc is an edge that has no arrow. Both ends of an undirected arc are equivalent--there is no head or tail. Therefor

HDU 3435 KM A new Graph Game

和HDU 3488一样的,只不过要判断一下是否有解. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 const int INF = 0x3f3f3f3f; 10 11 i

HDU 5876:Sparse Graph(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

hdu 5422 Rikka with Graph(简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has a non-direct graph with n vertices and m edges. The length of each edge is 1.

hdu 5424 Rikka with Graph II 哈密顿通路

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio