[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\) ...
随机推荐
- Xshell访问kali配置
1.安装虚拟机VMware Workstation12 PRO 2.在虚拟机上安装kali2.0 3.查看liunx的ip地址ifconfig 4.端口 协议 (1)RDP协议(桌面协议)3389端口 ...
- iOS NSSet 学习 “无序数组” & 去重 案例
“NSSet,NSMutableSet,和NSCountedSet类声明编程接口对象的无序集合(散列存储:在内存中的存储位置不连续). 而NSArray,NSDictionary类声明编程接口对象的有 ...
- linux环境变量 【转】
Linux 的变量可分为两类:环境变量和本地变量 环境变量,或者称为全局变量,存在与所有的shell 中,在你登陆系统的时候就已经有了相应的系统定义的环境变量了.Linux 的环境变量具有继承性,即子 ...
- 【leetcode刷题笔记】Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- hadoop实战项目:查找相同字母组成的字谜
前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...
- php的正则表达式
这篇文章介绍的内容是关于php的正则表达式 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下. 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配.替换.截取匹配 ...
- struts2实现文件的上传和下载实例[转]
实现原理 Struts 2是通过Commons FileUpload文件上传. Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器 ...
- 重置密码解决MySQL for Linux错误 ERROR 1045 (28000)
重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...
- 轻松掌握XMLHttpRequest对象------【这是.net 版本】
轻松掌握XMLHttpRequest对象 XmlHttp是什么? 最通用的定义为:XmlHttp是一套可以在Javascript.VbScript.Jscript等脚本语言中通过http协议传送或从接 ...
- git终端配置颜色
默认情况下git是黑白的. git config --global color.status auto git config --global color.diff auto git config - ...