PAT顶级(最大流)——1003 Universal Travel Sites (35 分)

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值