lightoj 1403 - Air Raid

题目大意就是说一个无环DAG图,现在有空降兵可以从天而降。图中有n个节点,每个节点必须经过且只能经过一次。

一个士兵可以沿着路一只走到尽头。。。

显然就是最小边覆盖,与二分图的最大匹配有关,还是很简单的。

有道升级版本lightoj1429。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 1010;
struct Hungary{
	int n, m;
	bool g[N][N];
	bool vis[N];
	int linker[N];
	vector<int> G[N];

	void Initation(int n,int m) {
		this->n = n, this->m = m;
		memset(g, 0,sizeof g);
		for (int i = 1;i <= n;++i)
			G[i].clear();
	}

	void Addedge(int u,int v) {
		g[u][v] = true;
		G[u].push_back(v);
	}

	bool Search(int u) {
		for (int i = 0;i < G[u].size(); ++i) {
			int v = G[u][i];
			if (g[u][v] && !vis[v]) {
				vis[v] = true;
				if (linker[v] == -1 || Search(linker[v])) {
					linker[v] = u;
					return true;
				}
			}
		}
		return false;
	}

	int solve() {
		int ans = 0;
		memset(linker, -1,sizeof linker);

		for (int i = 1;i <= n;++i) {
			memset(vis, false,sizeof vis);
			if (Search(i)) ans++;
		}
		return ans;
	}
}Hungary;
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	int n, m;
	scanf("%d",&t);
	while(t--) {
		scanf("%d%d",&n,&m);
		Hungary.Initation(n, n);
		int u, v;
		for (int i = 1;i <= m;++i) {
			scanf("%d%d",&u,&v);
			Hungary.Addedge(u, v);
		}
		printf("Case %d: %d\n", ++icase, n - Hungary.solve());
	}
	return 0;
}


你可能感兴趣的:(最小路径覆盖,lightoj)