1003 Universal Travel Sites (35 分)
解题思路:
没啥好说的,题目可能有点难懂,可以理解为最后到达目的地的人数最多为多少个,即最大流,最大流裸题,跑DINIC。
代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define G 6.67430*1e-11
#define rd read()
#define pi 3.1415926535
using namespace std;
const ll mod = 998244353;
const int MAX1 = 100005;
const int MAX2 = 300005;
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
map<string, vector<int> > head;
struct ss
{
string e;
int w;
};
vector<ss> v;
map<string, int> depth;
map<string, int> vis;
bool bfs(string s,string e)
{
queue<string> q;
depth.clear();
vis.clear();
vis[s];
q.push(s);
depth[s] = 1;
while (q.size())
{
string t = q.front();
for (int i = 0; i < head[t].size(); i++)
{
if (!vis[v[head[t][i]].e]&&v[head[t][i]].w)
{
depth[v[head[t][i]].e] = depth[t] + 1;
vis[v[head[t][i]].e] = 1;
q.push(v[head[t][i]].e);
}
}
q.pop();
}
return vis[e];
}
int dfs(string e, string now,int dist)
{
if (now == e)
{
return dist;
}
for (int i = 0; i < head[now].size(); i++)
{
if (depth[v[head[now][i]].e] == depth[now] + 1 && v[head[now][i]].w)
{
int p=dfs(e, v[head[now][i]].e, min(dist, v[head[now][i]].w));
if (p)
{
v[head[now][i]].w -= p;
v[head[now][i]^1].w += p;
return p;
}
}
}
return 0;
}
int main()
{
string s, e;
int n;
cin >> s >> e;
n = rd;
for (int i = 0; i < n; i++)
{
string s, e;
int w;
cin >> s >> e>>w;
ss a, b;
a.e = b.e = e;
a.w = w;
b.w = -w;
v.push_back(a);
v.push_back(b);
head[s].push_back(i*2);
head[e].push_back(i*2+1);
}
int ans = 0;
while (bfs(s, e))
{
int now = 0;
while (now = dfs(e, s, 1e9))
ans += now;
}
cout << ans;
return 0;
}