Codeforces 1307 D. Cow and Fields

Codeforces 1307 D. Cow and Fields_第1张图片
Codeforces 1307 D. Cow and Fields_第2张图片

题意:

给你一个无向图, k k k 个特殊点,你要在两个不同的特殊点直接连一条无向边,使得 1 − > n 1−>n 1>n 的最短路最长。

首先,让我们使用 B F S BFS BFS 查找从字段 1 1 1 n n n 到每个特殊字段的距离。 对于特殊字段 i i i,令 x i x_{i} xi 表示到节点1的距离, y i y_{i} yi 表示到 n n n 的距离。

我们要选择两个字段 a a a b b b 来最大化 m i n ( x a + y b , y a + x b ) min(x_{a} + y_{b},y_{a} + x_{b}) minxa+ybya+xb)。 不失一般性,假设 x a + y b ≤ y a + x b 。 x_{a} +y_{b}≤y_{a}+ x_{b}。 xa+ybya+xb

现在我们要使 x a + y b x_{a} + y_{b} xa+yb 最大化,以 x a − y a ≤ x b − y b x_{a}-y_{a}≤x_{b}-y_{b} xayaxbyb 为准。 这可以通过按 x i − y i x_{i}-y_{i} xiyi 排序并迭代 x x x 来完成,同时保留 y y y 的后缀最大数组以计算 m a x b > a y b max_{b> a}y_{b} maxb>ayb

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    if (a >= mod)
        a = a % mod + mod;
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll g = exgcd(b, a % b, x, y);
    ll t = x;
    x = y;
    y = t - a / b * y;
    return g;
}


int n, m, k;
int a[200005];

vector<int> edges[200005];

int dist[2][200005];
int q[200005];

void bfs(int *dist, int s)
{
    fill(dist, dist + n, inf);
    int qh = 0, qt = 0;
    q[qh++] = s;
    dist[s] = 0;
    while (qt < qh)
    {
        int x = q[qt++];
        for (int y : edges[x])
        {
            if (dist[y] == inf)
            {
                dist[y] = dist[x] + 1;
                q[qh++] = y;
            }
        }
    }
}

int main()
{
    sddd(n, m, k);
    rep(i, 0, k - 1)
    {
        sd(a[i]);
        a[i]--;
    }
    sort(a, a + k);
    rep(i, 0, m - 1)
    {
        int u, v;
        sdd(u, v);
        u--, v--;
        edges[u].pb(v);
        edges[v].pb(u);
    }
    bfs(dist[0], 0);
    bfs(dist[1], n - 1);
    vector<pair<int, int>> data;
    rep(i, 0, k - 1)
    {
        data.emplace_back(dist[0][a[i]] - dist[1][a[i]], a[i]); //emplace_back()函数向容器中中加入临时对象
    }
    sort(data.begin(), data.end());
    int best = 0;
    int maxn = -INF;
    for (auto it : data)
    {
        int a = it.se;
        best = max(best, maxn + dist[1][a]);
        maxn = max(maxn, dist[0][a]);
    }
    int ans = min(dist[0][n - 1], best + 1);
    pd(ans);
}

你可能感兴趣的:(CodeForces)