[TopCoder12727]FoxAndCity
题意
你有一张\(n\)点的无向图,每个点有一个点权\(w_i\)。图中原来存在一些边,你可以任意给这张图加上一些边。
记点\(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的更多相关文章
- TopCoder12727 「SRM590Hard」FoxAndCity 最小割离散变量模型
问题描述 一张 \(N\) 个点无向图,边权都为 \(1\) ,添加若干条边,最小化 \(\sum\limits_{1 \le i \le n,i \in N_{+}}{(a_i-b_i)^2}\). ...
- Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1
据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...
- topcoder srm 590 div1 (max_flow_template)
problem1 link 对于每一个,找到其在目标串中的位置,判断能不能移动即可. problem2 link 如果最后的$limit$为$11=(1011)_{2}$,那么可以分别计算值为$(10 ...
- Topcoder SRM 590 Fox And City
Link 注意到原图给的是一个无向连通图. 如果在原图中两点之间有一条无向边,那么这两点到\(1\)的距离之差不大于\(1\). 这个命题的正确性是显然的,我们考虑它的逆命题: 给定每个点到\(1\) ...
随机推荐
- $用python玩点有趣的数据分析——一元线性回归分析实例
Refer:http://python.jobbole.com/81215/ 本文参考了博乐在线的这篇文章,在其基础上加了一些自己的理解.其原文是一篇英文的博客,讲的通俗易懂. 本文通过一个简单的例子 ...
- 020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar
1.Eclipse中无插件运行MP程序 1)在Eclipse中编写MapReduce程序 2)打包成jar包 3)使用FTP工具,上传jar到hadoop 集群环境 4)运行 2.具体步骤 说明:该程 ...
- 【HackerRank】 Find Digits
Find Digits Problem Statement Given a number you have to print how many digits in that number exactl ...
- python中类(class)和实例(instance)
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- Nginx配置指令的执行顺序
rewrite阶段 rewrite阶段是一个比较早的请求处理阶段,这个阶段的配置指令一般用来对当前请求进行各种修改(比如对URI和URL参数进行改写),或者创建并初始化一系列后续处理阶段可能需要的Ng ...
- Pandas的 loc iloc ix 区别
先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: ...
- TypeScript手册1 - 基本类型和接口
基本类型 像基本的JavaScript一样,TypeScript支持numbers, strings, structures, boolean等基本类型,而扩展的enum等类型,为编程提供了更多帮助. ...
- Difference between stem and lemma
lemma与stem的区别 Difference between stem and lemma 先从wikipedia上看看什么是stem,什么是lemma? Lemma(morphology):In ...
- Java日期时间输出格式优化
使用printf格式化日期 printf 方法可以很轻松地格式化时间和日期.使用两个字母格式,它以 %t 开头并且以下面表格中的一个字母结尾. 转 换 符 说 明 示 例 c 包括全部 ...
- nova shelve
当一个虚机不需要使用的时候,可以将其 shelve 起来.该操作会创建该虚机的一个快照并传到 Glance 中,然后在 Hypervisor 上将该虚机删除,从而释放其资源. 其主要过程为: dest ...