【Codeforces536D】Tavas in Kansas

Keywords: less iOS

Title:

  • Give you an undirected graph with n points and m edges. Each point has a point weight pi.
  • There are two people playing the game. They are at points s and t respectively.
  • Two people operate in turn. Each operation selects a non-negative integer x, which means that the point weight of the point whose shortest distance is less than or equal to X is taken away. (The point weight disappears after taking away, and the number of points with a little weight is at least 1 at a time.)
  • Ask about the final winner or loser (whose total power is greater).
  • n≤2000,n−1≤m≤100000

Explanation:

  • Firstly, the shortest distance from each point to s and t, ds[i],dt[i], i s obtained by running the shortest path.
  • Then the problem is transformed into a two-dimensional space with n points (ds[i],dt[i]). Every time a person chooses a line parallel to the x or y axis, he or she will take away the points on the left or below and make sure that at least one point is taken away at a time.
  • First, it can be discretized.
  • Then transfer from late to early according to the time.
  • dp[i][j][k] represents the maximum or minimum weight difference of the point at the lower left of (i,j) before a certain operation of the K person. According to the positive or negative of dp[0][0][0], the final victory or defeat can be judged.
  • Then you can move from row to column and discuss if the row is somewhat or if it is the first row or column of a particular choice.
  • Time complexity O((n+m)logn+n2)

Code:

#include <bits/stdc++.h>
#define gc getchar()
#define ll long long
#define N 2009
#define inf 0x3f
using namespace std;
ll n,m,s,t,dis[N],Dis[N],vis[N],r,c,val[N],f[N][N];
ll S[N][N],T[N][N],dp[N][N][2];
vector <ll> X,Y;
int main()
{
    #ifdef ONLINE_JUDGE
    bool lych_cys_is_zero=true,miaom_is_zero=true;
    #else
    freopen("1.in","r",stdin);
    #endif
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>m>>s>>t;
    for (ll i=1;i<=n;i++) cin>>val[i];
    memset(f,inf,sizeof(f));
    for (ll i=1;i<=m;i++)
    {
        ll x,y,z;
        cin>>x>>y>>z;
        f[x][y]=f[y][x]=min(z,f[x][y]);
    }
    memset(dis,inf,sizeof(dis));
    memset(Dis,inf,sizeof(Dis));
    dis[s]=Dis[t]=0;
    memset(vis,0,sizeof(vis));
    for (ll i=1;i<=n;i++)
    {
        ll k=-1;
        for (ll j=1;j<=n;j++)
            if (!vis[j]&&(dis[j]<dis[k]||k==-1)) k=j;
        vis[k]=1;
        for (ll j=1;j<=n;j++)
            if (!vis[j]&&f[k][j]<(ll)1e18&&dis[k]+f[k][j]<dis[j]) dis[j]=dis[k]+f[k][j];
    }
    memset(vis,0,sizeof(vis));
    for (ll i=1;i<=n;i++)
    {
        ll k=-1;
        for (ll j=1;j<=n;j++)
            if (!vis[j]&&(Dis[j]<Dis[k]||k==-1)) k=j;
        vis[k]=1;
        for (ll j=1;j<=n;j++)
            if (!vis[j]&&f[k][j]<(ll)1e18&&Dis[k]+f[k][j]<Dis[j]) Dis[j]=Dis[k]+f[k][j];
    }
    X.clear(),Y.clear();
    for (ll i=1;i<=n;i++) X.push_back(dis[i]),Y.push_back(Dis[i]);
    sort(X.begin(),X.end()),sort(Y.begin(),Y.end());
    X.resize(unique(X.begin(),X.end())-X.begin());
    Y.resize(unique(Y.begin(),Y.end())-Y.begin());
    memset(S,0,sizeof(S));
    memset(T,0,sizeof(T));
    memset(dp,0,sizeof(dp));
    for (ll i=1;i<=n;i++)
    {
        dis[i]=lower_bound(X.begin(),X.end(),dis[i])-X.begin()+1;
        Dis[i]=lower_bound(Y.begin(),Y.end(),Dis[i])-Y.begin()+1;
        S[dis[i]][Dis[i]]+=val[i],T[dis[i]][Dis[i]]++;
    }
    for (int i=1;i<=n;i++)
    {
        cerr<<dis[i]<<" "<<Dis[i]<<endl;
    }
    n++;
    for (ll i=1;i<=n;i++)
        for (ll j=1;j<=n;j++)
        {
            S[i][j]+=S[i-1][j]+S[i][j-1]-S[i-1][j-1];
            T[i][j]+=T[i-1][j]+T[i][j-1]-T[i-1][j-1];
        }
    for (ll i=n-1;i>=0;i--)
        for (ll j=n-1;j>=0;j--)
        {
            if (T[i+1][n]-T[i][n]-T[i+1][j]+T[i][j]==0)
                dp[i][j][0]=dp[i+1][j][0];
            else
                dp[i][j][0]=max(dp[i+1][j][0],dp[i+1][j][1])+S[i+1][n]-S[i][n]-S[i+1][j]+S[i][j];
            if (T[n][j+1]-T[n][j]-T[i][j+1]+T[i][j]==0)
                dp[i][j][1]=dp[i][j+1][1];
            else
                dp[i][j][1]=min(dp[i][j+1][1],dp[i][j+1][0])-S[n][j+1]+S[n][j]+S[i][j+1]-S[i][j];
        }
    cerr<<dp[0][0][0]<<endl;
    if (dp[0][0][0]>0) cout<<"Break a heart"<<endl;
    else if (dp[0][0][0]<0) cout<<"Cry"<<endl;
    else cout<<"Flowers"<<endl;
    return 0;
}

Posted by johnthedeveloper on Fri, 04 Jan 2019 09:27:10 -0800