[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\) ...
随机推荐
- 【渗透神器系列】Fiddler (收藏)
发表于 2017-04-27 | 分类于 安全工具 | | 阅读次数 593 人世起起落落 左手边上演的华灯初上 右手边是繁华落幕的星点余光 本篇作为渗透神器系列第二篇,将介绍 ...
- Oracle网络服务管理与配置
一.Oracle网络服务概述 1.网络解决方案. (1)可连接性:在Oracle中,由Oracle net组件负责在客户端应用程序与数据服务器之间创建会话.维护会话连接和数据传输. (2)可管理性: ...
- ERROR 2003 (HY000): Can't connect to MySQL server on "" (113)
服务器为centos6. 这个原因是因为防火墙的问题 在mysql服务端执行 service iptables stop chkconfig iptables off #永久关闭防火墙 看情况执行 然 ...
- 混淆Android JAR包的方法
http://blog.csdn.net/vurtne_ye/article/details/35599491 1)导出jar包 如何在eclipse上Android工程中导出jar包?google, ...
- codeforces上某题
一道codeforces上的题目. 题目大意: 定义有k个不同的字符的字符串为好字符串.现在给出一个字符串,求解对该字符串的每个前缀Si至少是多少个好字符串的连接,若不能由好字符串连接而成则输出-1. ...
- 20145239杜文超《网络对抗》- Web基础
20145239杜文超<网络对抗>- Web基础 基础问题回答 1.什么是表单? 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输 ...
- 通过加载Xib文件来创建UITableViewCell造成复用数据混乱问题方案
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- python中的元类metaclass
本文是一个转载的,因为原文写的太好了,所以直接copy过来吧. 原文请看:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上 很热的帖子.提问 ...
- Freemarker中大于号>的使用
在Freemarker中,比较数据的大小时候,要注意大于号(>)的使用.如果不注意,程序就会发生异常信息,如下面的例子: 1 2 3 4 <#assign x = 4> < ...
- HBase-scan简介及优化(缓存与批量处理)
扫描(scan) 这种技术类似于数据库系统中的游标(cursor),并利用到了HBase提供的底层顺序存储的数据结构. 扫描操作的使用跟get方法非常类似.由于扫描操作的工作方式类似于迭代器,所以用户 ...