Problem Statement

     There is a country with n cities, numbered 0 through n-1. City 0 is the capital. The road network in the country forms an undirected connected graph. In other words: Some pairs of cities are connected by bidirectional roads. For every city there is at least one sequence of consecutive roads that leads from the city to the capital. (Whenever two roads need to cross outside of a city, the crossing is done using a bridge, so there are no intersections outside of the cities.) You are given a String[] linked that describes the road network. For each i and j, linked[i][j] is 'Y' if the cities i and j are already connected by a direct road, and it is 'N' otherwise. The distance between two cities is the smallest number of roads one needs to travel to get from one city to the other. The people living outside of the capital are usually unhappy about their distance from the capital. You are also given a int[] want with n elements. For each i, want[i] is the desired distance between city i and the capital (city 0). Fox Ciel is in charge of building new roads. Each new road must again be bidirectional and connect two cities. Once the new roads are built, the citizens will evaluate how unhappy they are with the resulting road network: For each i: Let real[i] be the new distance between city i and the capital. Then the people in city i increase the unhappiness of the country by (want[i] - real[i])^2. Return the minimal total unhappiness Ciel can achieve by building some (possibly zero) new roads.

题目大意:给定n个点的无向图,边权均为1,每个点有一个属性wi,现在可以在图中任意加边,记加边后每个点到1号点的距离为di,最小化Σ(wi - di)^2.

样例:

Sample Input

3

NYN

YNY

NYN

0 1 1

4

NYNN

YNYN

NYNY

NNYN

0 3 3 3

6

NYNNNY

YNYNNN

NYNYNN

NNYNYN

NNNYNY

YNNNYN

0 2 2 2 2 2

3

NYY

YNN

YNN

0 0 0

6

NYNNNN

YNYNNN

NYNYYY

NNYNYY

NNYYNY

NNYYYN

0 1 2 3 0 3

6

NYNNNN

YNYNNN

NYNYYY

NNYNYY

NNYYNY

NNYYYN

0 1 2 4 0 4

11

NYNYYYYYYYY

YNYNNYYNYYY

NYNNNYYNYYN

YNNNYYYYYYY

YNNYNYYYNYY

YYYYYNNYYNY

YYYYYNNNYYY

YNNYYYNNNYY

YYYYNYYNNNY

YYYYYNYYNNY

YYNYYYYYYYN

0 1 2 0 0 5 1 3 0 2 3

Sample Output

0

5

2

3

6

28

分析:综合了许多知识的好题.

   题目说可以任意加边,那么是不是就意味着每个点的最短路都是任意的呢? 显然不是的,考虑确定每个点的最短路有什么限制.

   首先,d1 = 0.  然后对于任意有边的一对点(i,j),|di - dj| ≤ 1. 现在要求满足上述限制的最值.

   这其实就是个离散变量模型,和bzoj3144类似. 先拆点,S向i号点拆出的0号点连容量为inf的边,i号点拆出的n-1号点向T连容量为inf的边. i号点拆出的k号点向k+1号点连容量为(a[i] - k - 1) ^ 2的边.

   对于有限制的点对(i,j),i拆出的第k个点向j拆出的第k-1个点连容量为inf的边,j连i也同样如此.

   还没完.必须保证d1 = 0.那么1拆出的第k个点向第k+1个点的边就不能被割. 将其容量变成inf就好了. 但是这样会存在一个问题:ST总是连通的. 因为S到T总是可以经过1号点拆出的点,这条路径上的每条边的容量都是inf,割不掉.

   怎么解决呢?去掉S与1号点拆出的第一个点的连边就好了.

   不断调整,满足最小割的要求,同时使得答案合理,这是最小割模型建图的一般分析方法.

   同时这道题融合了最短路问题的一些技巧,例如hdu5385,每个点到源点的最短路都和与它相连的点的最短路有关.

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7fffffff;
int n,a[],id[][],cnt,head[maxn],to[maxn],nextt[maxn],w[maxn],tot = ;
int d[maxn],S,T,ans;
char s[][]; void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++; w[tot] = ;
to[tot] = x;
nextt[tot] = head[y];
head[y] = tot++;
} bool bfs()
{
memset(d,-,sizeof(d));
d[S] = ;
queue <int> q;
q.push(S);
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == T)
return true;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (w[i] && d[v] == -)
{
d[v] = d[u] + ;
q.push(v);
}
}
}
return false;
} int dfs(int u,int f)
{
if (u == T)
return f;
int res = ;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (w[i] && d[v] == d[u] + )
{
int temp = dfs(v,min(f - res,w[i]));
w[i] -= temp;
w[i ^ ] += temp;
res += temp;
if (res == f)
return res;
}
}
if (!res)
d[u] = -;
return res;
} void dinic()
{
while(bfs())
ans += dfs(S,inf);
} int main()
{
scanf("%d",&n);
for (int i = ; i <= n; i++)
scanf("%s",s[i] + );
for (int k = ; k <= n - ; k++)
for (int i = ; i <= n; i++)
id[i][k] = ++cnt;
S = cnt + ;
T = S + ;
add(id[][n - ],T,inf);
for (int i = ; i <= n; i++)
add(S,id[i][],inf),add(id[i][n - ],T,inf);
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
{
if (s[i][j] == 'Y')
{
for (int k = ; k <= n - ; k++)
add(id[i][k],id[j][k - ],inf);
}
}
for (int i = ; i <= n; i++)
{
int x;
scanf("%d",&x);
for (int j = ; j <= n - ; j++)
add(id[i][j],id[i][j + ],(i == ? inf : (x - j - ) * (x - j - )));
}
dinic();
printf("%d\n",ans); return ;
}

Topcoder SRM590 Fox And City的更多相关文章

  1. Topcoder SRM 590 Fox And City

    Link 注意到原图给的是一个无向连通图. 如果在原图中两点之间有一条无向边,那么这两点到\(1\)的距离之差不大于\(1\). 这个命题的正确性是显然的,我们考虑它的逆命题: 给定每个点到\(1\) ...

  2. HDU.5385.The path(构造)

    题目链接 最短路构造题三连:这道题,HDU4903,SRM590 Fox And City. \(Description\) 给定一张\(n\)个点\(m\)条边的有向图,每条边的边权在\([1,n] ...

  3. 未A,或用水法,或不熟的题

    今天是2017.11.25 1. 用栈实现dfs JZOJ_senior 3467 2. 链表加堆或线段树乱搞 JZOJ_senior 3480 3. 求每个边所在的奇环.偶环 JZOJ_senior ...

  4. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  5. Topcoder 练习小记,Java 与 Python 分别实现。

    Topcoder上的一道题目,题目描述如下: Problem Statement      Byteland is a city with many skyscrapers, so it's a pe ...

  6. [TopCoder] SRM_594_DIV2.250

    好长一段时间没写博客了,实在是想不出有什么好写的.近期也有对自己的职业做了一点思考,还是整理不出个所以然来,很是烦躁 ... 研究TopCoder已经有一小段时间了,都是在做之前的题目,还没有实际参加 ...

  7. TopCoder入门

    TopCoder入门 http://acmicpc.info/archives/164?tdsourcetag=s_pctim_aiomsg 本文根据经典的TC教程完善和改编.TopCoder:htt ...

  8. BZOJ 2001: [Hnoi2010]City 城市建设

    2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1132  Solved: 555[Submit][ ...

  9. History lives on in this distinguished Polish city II 2017/1/5

    原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...

随机推荐

  1. ES系列二、CentOS7安装ES head6.3.1

    1.Head插件简介 ElasticSearch-head是一个H5编写的ElasticSearch集群操作和管理工具,可以对集群进行傻瓜式操作. 显示集群的拓扑,并且能够执行索引和节点级别操作 搜索 ...

  2. 017_mac格式化硬盘,mac如何格式化硬盘

    想做一个mac和windows都能识别的系统,推荐设置成什么格式 一.在mac下格式化 在Mac 下,打开右下角应用程序-实用工具-磁盘工具,里面选取你的移动硬盘,然后进行格式化,设置成EXFat格式 ...

  3. 001_ansible通过堡垒机登录

    一. 之前一直通过跳板机登录线上服务器,ssh可以的,如下图所示 vim ~/.ssh/config ssh xx.xx.xx.xx线上服务器是可以的,但是ansible执行显示目标主机不可达,其实a ...

  4. 强大的Js树型控件Dtree使用详解

    http://www.lmwlove.com/ac/ID868 在学习文章之前,要学会看官方网站http://destroydrop.com/javascripts/tree.从官方页面你能知道:dt ...

  5. 转载:gc的概念,如果A和B对象循环引用,是否可以被GC?

    原文:https://www.cnblogs.com/zhchoutai/p/6784929.html ①首先说一下,GC里边在JVM其中是使用的ROOT算法,ROOT算法,什么称作为ROOT呢,就是 ...

  6. Java中日期格式化SimpleDateFormat类包含时区的处理方法

    1.前言 需要把格式为“2017-02-23T08:04:02+01:00”转化成”23-02-2017-T15:04:02“格式(中国时区为+08:00所以是15点),通过网上查找答案,发现没有我需 ...

  7. Android动态控制状态栏显示和隐藏

    记得之前有朋友在留言里让我写一篇关于沉浸式状态栏的文章,正巧我确实有这个打算,那么本篇就给大家带来一次沉浸式状态栏的微技巧讲解. 其实说到沉浸式状态栏这个名字我也是感到很无奈,真不知道这种叫法是谁先发 ...

  8. as无法在vivo上安装程序解决

    1. vivo手机真的很麻烦,首先要确定vivo账号密码,允许安装后还一直失败.记录下解决方案 在工程目录的gradle.properties中添加android.injected.testOnly ...

  9. Gradient Domain Guided Image Filtering(梯度域导向滤波)

    作者提出了一种新的梯度域引导图像滤波器,通过将明确的一阶边缘感知约束结合到现有的引导图像滤波器中. matlab代码实现 转载至:https://blog.csdn.net/majinlei121/a ...

  10. LeetCode(40):组合总和 II

    Medium! 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数 ...