2020 ICPC Asia East Continent Final D. City Brain(最短路+三分)

传送门

题意:

给出\(n\)​​ 个点,\(m\)条边的无向带权图,初始边权都为\(1\),一共有\(k\) 次操作机会,每次操作可以选择一条边使其边权\(+1\), 通过一条边的时间为 \(1/\)边权 ,求\(min(dis(s1,t1)+dis(s2,t2))\)

题解:

考虑枚举两条路径的公共起点和终点,假设长度 \(x\) , 对于剩下的各自走的边长度为 \(y\) ,贪心的考虑,肯定是将操作次数均分最好,即对于长度为\(l\) ,操作次数为 \(t\) ,最优时间肯定是 \((t\%l)/(2+t/l)+(l-t\%l)/(1+t/l)\)

然后考虑三分公共路径上分到的操作次数 ,时间复杂度为\(O(n^2logn)\) ,会\(tle\)

再贪心的想,对于公共长度固定,那肯定是让各自长度越小越好,所有可以先\(n^2\) 跑最短路,然后预处理出每个公共长度的最小各自长度 ,最后再三分即可。

代码:

#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=5e3+5;
const int inf=0x3f3f3f3f;
int dis[MAXN][MAXN],vis[MAXN];
vector<int> g[MAXN];
int dp[MAXN];
int n, m, k;
void bfs(int u)
{
    dis[u][u] = 0;
    queue<pii> q;
    q.push({ u, 0 });
    vis[u] = 1;
    while(!q.empty())
    {
        pii now = q.front();
        q.pop();
        for(auto v:g[now.first])
        {
            if(vis[v])
                continue;
            dis[u][v] = dis[u][now.first] + 1;
            vis[v] = 1;
            q.push({ v, dis[u][v] });
        }
    }
}
bool ok(int s,int i,int j,int t)
{
    if(dis[s][i]!=inf&&dis[i][j]!=inf&&dis[j][t]!=inf)
        return true;
    else
        return false;
}
double get_cost(int x,int y)
{
    int c1 = 1 + y / x;
    int c2 = 2 + y / x;
    double ans1 = 1.0 * (x - y % x) / c1;
    double ans2 = 1.0 * (y % x) / c2;
    return ans1 + ans2;
}
double cal(int x,int y)
{
    if(!x&&!y)
        return 0;
    else if(!x)
        return 2*get_cost(y,k);
    else if(!y)
        return get_cost(x,k);
    else
    {
        double ans = 1e9;
        int l = 0, r = k;
        while(l<=r)
        {
            int mid1 = l + (r - l) / 3;
            int mid2 = r - (r - l) / 3;
            double v1 = 2 * get_cost(y, mid1) + get_cost(x, k - mid1);
            double v2 = 2 * get_cost(y, mid2) + get_cost(x, k - mid2);
            if(v1<v2)
            {
                ans = v1;
                r = mid2-1;
            }
            else 
            {
                ans = v2;
                l = mid1+1;
            }
        }
        return ans;
    }
}
int main()
{
    memset(dis, inf, sizeof dis);
    memset(dp, inf, sizeof dp);
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= m;i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int s1, t1, s2, t2;
    scanf("%d%d%d%d", &s1, &t1, &s2, &t2);
    for (int i = 1; i <= n;i++)
    {
        for (int j = 1; j <= n;j++)
            vis[j] = 0;
        bfs(i);
    }
    //cout << 1 << endl;
    dp[0] = dis[s1][t1] + dis[s2][t2];
    for (int i = 1; i <= n;i++)
    {
        for (int j = 1; j <= n;j++)
        {
            if ((ok(s1, i, j, t1) || ok(s1, j, i, t1)) && (ok(s2, i, j, t2) || ok(s2, j, i, t2)))
            {
                int d = dis[i][j];
                dp[d] = min(dp[d], min(dis[s1][i] + dis[j][t1], dis[s1][j] + dis[i][t1]) + min(dis[s2][i] + dis[j][t2], dis[s2][j] + dis[i][t2]));
            }
        }
    }
    double ans = 1e9;
    //cout << 1 << endl;
    for (int i = 0; i <= n;i++)
    {
        if(dp[i]==inf)
            continue;
        ans = min(ans, cal(dp[i], i));
    }
    printf("%.15lf\n", ans);
}
上一篇:PARTITION BY ORDER BY组合使用


下一篇:Codeforces 521E - Cycling City(点双连通分量+分类讨论)