//1151的两种做法,注释为匈牙利算法 DAG图的最小路径覆盖数=节点数(n)- 最大匹配数(m)
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int N = 150;
vector<int> mp[N];
// int pre[N];
// bool flag[N];
// int find(int cur)
// {
// int i, k;
// for (i = 0; i < mp[cur].size(); i++)
// {
// k = mp[cur][i];
// if (!flag[k])
// {
// flag[k] = true;
// if (pre[k] == -1 || find(pre[k]))
// {
// pre[k] = cur;
// return 1;
// }
// }
// }
// return 0;
// }
int nodeA, nodeB;
int disx[N], disy[N], matx[N], maty[N], que[N];
bool bfs()
{
int i, j, fore = 0, rear = 0;
bool flag = false;
vector<int>::iterator ite;
for (i = 0; i < nodeA; ++i) //找出中还没有匹配的点
{
if (matx[i] == -1) que[rear++] = i;
disx[i] = 0;
}
for (i = 0; i < nodeB; ++i) disy[i] = 0;
while (fore < rear) //从这些没有匹配的点里,找路径
{
int x = que[fore++];
for (ite = mp[x].begin(); ite != mp[x].end(); ++ite)
{
if (disy[*ite] == 0)
{
disy[*ite] = disx[x] + 1;
if (maty[*ite] == -1)
flag = true; //找到一条路径,则返回值是正的
else //找不到路径,则把前向点如队列
{
disx[maty[*ite]] = disy[*ite] + 1;
que[rear++] = maty[*ite];
}
}
}
}
return flag;
}
bool dfs(int x)
{
vector<int>::iterator ite;
for (ite = mp[x].begin(); ite != mp[x].end(); ++ite)
{
if (disy[*ite] == disx[x] + 1)
{
disy[*ite] = 0;
if (maty[*ite] == -1 || dfs(maty[*ite]))
{
matx[x] = *ite;
maty[*ite] = x;
return 1;
}
}
}
return false;
}
int Hopcroft_Karp()
{
int i;
int match = 0;
memset(matx, -1, sizeof(matx));
memset(maty, -1, sizeof(maty));
while (bfs())
{
for (i = 0; i < nodeA; ++i)
{
if (matx[i] == -1 && dfs(i))
++match;
}
}
return match;
}
int main()
{
#ifdef DeBUG
freopen("//home//amb//桌面//1.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
int l;
int n;
scanf("%d%d", &l, &n);
for (int i = 0; i <= l; i++)
{
mp[i].clear();
}
int a, b;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &a, &b);
mp[a-1].push_back(b-1);
// mp[b-1].push_back(a-1);
}
nodeA=l;
nodeB=l;
// int sum = 0;
// memset(pre, -1, sizeof(pre));
// for (int i = 1; i <= l; i++)
// {
// memset(flag, false, sizeof(flag));
// if (find(i))
// {
// sum++;
// printf("%d\n", i);
// }
// }
printf("%d\n", l - Hopcroft_Karp());
}
return 0;
}
2389转自网络,看到请联系我~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iterator>
using namespace std;
struct NodeGuest
{
double x, y, speed;
} guest[3010];
struct NodeNmbre
{
double x, y;
} LocatonOfshan[3010];
int nodeA, nodeB;
int disx[3010], disy[3010], matx[3010], maty[3010], que[3010];
vector<int> mp[3010];
bool bfs()
{
int i, j, fore = 0, rear = 0;
bool flag = false;
vector<int>::iterator ite;
for (i = 0; i < nodeA; ++i) //找出中还没有匹配的点
{
if (matx[i] == -1) que[rear++] = i;
disx[i] = 0;
}
for (i = 0; i < nodeB; ++i) disy[i] = 0;
while (fore < rear) //从这些没有匹配的点里,找路径
{
int x = que[fore++];
for (ite = mp[x].begin(); ite != mp[x].end(); ++ite)
{
if (disy[*ite] == 0)
{
disy[*ite] = disx[x] + 1;
if (maty[*ite] == -1)
flag = true; //找到一条路径,则返回值是正的
else //找不到路径,则把前向点如队列
{
disx[maty[*ite]] = disy[*ite] + 1;
que[rear++] = maty[*ite];
}
}
}
}
return flag;
}
bool dfs(int x)
{
vector<int>::iterator ite;
for (ite = mp[x].begin(); ite != mp[x].end(); ++ite)
{
if (disy[*ite] == disx[x] + 1)
{
disy[*ite] = 0;
if (maty[*ite] == -1 || dfs(maty[*ite]))
{
matx[x] = *ite;
maty[*ite] = x;
return 1;
}
}
}
return false;
}
int Hopcroft_Karp()
{
int i;
int match = 0;
memset(matx, -1, sizeof(matx));
memset(maty, -1, sizeof(maty));
while (bfs())
{
for (i = 0; i < nodeA; ++i)
{
if (matx[i] == -1 && dfs(i))
++match;
}
}
return match;
}
double getTime(int a, int b)
{
double dis = sqrt( (guest[a].x - LocatonOfshan[b].x) * (guest[a].x - LocatonOfshan[b].x) + (guest[a].y - LocatonOfshan[b].y) * (guest[a].y - LocatonOfshan[b].y));
return dis / guest[a].speed;
}
int main()
{
// freopen("//home//amb//桌面//1.in", "r", stdin);
int T;
double t;
int ca = 1;
scanf("%d", &T);
while (T--)
{
scanf("%lf %d", &t, &nodeA);
int i, j;
for (i = 0; i < nodeA; ++i)
scanf("%lf %lf %lf", &guest[i].x, &guest[i].y, &guest[i].speed);
scanf("%d", &nodeB);
for (i = 0; i < nodeB; ++i)
scanf("%lf %lf", &LocatonOfshan[i].x, &LocatonOfshan[i].y);
for (i = 0; i < nodeA; ++i)
mp[i].clear();
for (i = 0; i < nodeA; ++i)
{
for (j = 0; j < nodeB; ++j)
{
double tt = getTime(i, j);
if (tt <= t)
mp[i].push_back(j);
}
}
printf("Scenario #%d:\n%d\n\n", ca++, Hopcroft_Karp());
}
return 0;
}