码迷,mamicode.com
首页 > 其他好文 > 详细

[TopCoder12727]FoxAndCity

时间:2018-04-05 23:14:38      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:net   val   ref   cto   name   etc   source   题意   empty   

vjudge

题意

你有一张\(n\)点的无向图,图中存在一些边,你可以任意给这张图加上一些边。
记点\(i\)到点\(1\)的距离为\(d_i\),你需要最小化\(\sum_{i=1}^{n}(w_i-d_i)^2\)

sol

第一次做\(Topcoder\)的题。。。
很像[HNOI2013]切糕那个题。对于每对原图中有边相连的点,从\(k\)位置向对方的\(k-1\)位置连\(inf\)边。

code

\(Topcoder\)上面要写一个\(class\)什么鬼的。
直接蒯吧。。。(我也是直接蒯的别人的)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
const int N = 2000;
const int inf = 1e9;
struct edge{int to,nxt,w;}a[N<<7];
int n,val[N],S,T,head[N],cnt=1,dep[N],cur[N];
char s[45][45];
queue<int>Q;
void link(int u,int v,int w)
{
    a[++cnt]=(edge){v,head[u],w};
    head[u]=cnt;
    a[++cnt]=(edge){u,head[v],0};
    head[v]=cnt;
}
bool bfs()
{
    memset(dep,0,sizeof(dep));
    dep[S]=1;Q.push(S);
    while (!Q.empty())
    {
        int u=Q.front();Q.pop();
        for (int e=head[u];e;e=a[e].nxt)
            if (a[e].w&&!dep[a[e].to])
                dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
    }
    return dep[T];
}
int dfs(int u,int f)
{
    if (u==T) return f;
    for (int &e=cur[u];e;e=a[e].nxt)
        if (a[e].w&&dep[a[e].to]==dep[u]+1)
        {
            int tmp=dfs(a[e].to,min(a[e].w,f));
            if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
        }
    return 0;
}
int Dinic()
{
    int res=0;
    while (bfs())
    {
        for (int i=1;i<=T;++i) cur[i]=head[i];
        while (int tmp=dfs(S,inf)) res+=tmp;
    }
    return res;
}
int yyb_solve()
{
    S=n*n+1;T=S+1;
    for (int i=1;i<=n;++i)
    {
        link(S,(i-1)*n+1,i==1?0:inf);
        for (int j=1;j<n;++j) link((i-1)*n+j,(i-1)*n+j+1,i==1?inf:(val[i]-j)*(val[i]-j));
        link(i*n,T,inf);
        for (int j=1;j<=n;++j)
            if (s[i][j]=='Y')
                for (int k=1;k<n;++k)
                    link((i-1)*n+k+1,(j-1)*n+k,inf);
    }
    return Dinic();
}
/*
int main()
{
    n=gi();
    for (int i=1;i<=n;++i) scanf("%s",s[i]+1);
    for (int i=1;i<=n;++i) val[i]=gi();
    printf("%d\n",yyb_solve());return 0;
}
*/
class FoxAndCity{
public:
    int minimalCost(vector<string>mp,vector<int>vv)
        {
            n=mp.size();
            for (int i=1;i<=n;++i)
                for (int j=1;j<=n;++j)
                    s[i][j]=mp[i-1][j-1];
            for (int i=1;i<=n;++i) val[i]=vv[i-1];
            return yyb_solve();
        }
};

[TopCoder12727]FoxAndCity

标签:net   val   ref   cto   name   etc   source   题意   empty   

原文地址:https://www.cnblogs.com/zhoushuyu/p/8724609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!