这题显然是一个最小斯坦纳树的模型,直接上模板即可,就是正六边形比较恶心。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 0x3f3f3f3f
using namespace std;
const int N=1610,M=9610;
typedef pair<int,int>P;
int R,n,m,k=4,f[N][1<<4],st[N],all=1<<k,ans,g[N],to[M],nxt[M],cost[N],e,id[41][41];
bool vis[N][1<<4];
queue<int>Q;
int read(){
char ch;
while(!(((ch=getchar())=='A')||(ch=='B')||(ch=='C')||(ch=='D')||(ch=='.')));
return ch=='.'?4:ch-'A';
}
void add(int u,int v){to[e]=v;nxt[e]=g[u];g[u]=e++;}
void add(int u,int x,int y){
if(x<0||y<0||x>=m||y>=m)return;
if(~id[x][y]&&id[x][y]!=u)add(u,id[x][y]);
}
void spfa(int S){
while(!Q.empty()){
int u=Q.front();Q.pop();
vis[u][S]=false;
for(int p=g[u];~p;p=nxt[p]){
int v=to[p];
int w=cost[v];
if(f[v][st[v]|S]>f[u][S]+w){
f[v][st[v]|S]=f[u][S]+w;
if(st[v]|S!=S||vis[v][S])continue;
vis[v][S]=1;
Q.push(v);
}
}
}
}
void solve(){
memset(g,-1,sizeof g);
while(!Q.empty())Q.pop();
e=0;n=k;m=R*2-1;
rep(i,m)rep(j,m)id[i][j]=-1;
rep(i,R-1)rep(j,R+i){
int x=read();
if(x<k)cost[id[i][j]=x]=0;else cost[id[i][j]=n++]=1;
}
rep(i,R)rep(j,m-i){
int x=read();
if(x<k)cost[id[R+i-1][j]=x]=0;else cost[id[R+i-1][j]=n++]=1;
}
rep(i,m)rep(j,m)if(~id[i][j]){
add(id[i][j],i,j-1);
add(id[i][j],i,j+1);
if(i<R-1){
add(id[i][j],i-1,j-1);
add(id[i][j],i-1,j);
add(id[i][j],i+1,j);
add(id[i][j],i+1,j+1);
}else if(i==R-1){
add(id[i][j],i-1,j-1);
add(id[i][j],i-1,j);
add(id[i][j],i+1,j-1);
add(id[i][j],i+1,j);
}else{
add(id[i][j],i-1,j);
add(id[i][j],i-1,j+1);
add(id[i][j],i+1,j-1);
add(id[i][j],i+1,j);
}
}
rep(i,n)rep(j,all)f[i][j]=INF;
memset(st,0,sizeof st);
memset(vis,0,sizeof vis);
rep(i,k)st[i]=1<<i,f[i][st[i]]=0;
for(int j=1;j<all;j++){
rep(i,n){
if(st[i]&&(st[i]&j)==0)continue;
for(int sub=(j-1)&j;sub;sub=(sub-1)&j){
int x=st[i]|sub,y=st[i]|(j-sub);
f[i][j]=min(f[i][j],f[i][x]+f[i][y]-cost[i]);
}
if(f[i][j]<INF){
Q.push(i);
vis[i][j]=true;
}
}
spfa(j);
}
ans=INF;
rep(j,n)ans=min(ans,f[j][all-1]);
printf("You have to buy %d parcels.\n",ans);
}
int main(){
while(scanf("%d",&R),R)solve();
return 0;
}

  

BZOJ1401 : Hexagon的更多相关文章

  1. Gerald's Hexagon

    Gerald's Hexagon Gerald got a very curious hexagon for his birthday. The boy found out that all the ...

  2. Codeforces Round #313 (Div. 1) A. Gerald's Hexagon

    Gerald's Hexagon Problem's Link: http://codeforces.com/contest/559/problem/A Mean: 按顺时针顺序给出一个六边形的各边长 ...

  3. UVALive 6124 Hexagon Perplexagon 题解

    http://vjudge.net/problem/viewProblem.action?id=37480 East Central Regional Contest Problem C: Hexag ...

  4. Codeforces Round #313 (Div. 2) C. Gerald's Hexagon 数学

    C. Gerald's Hexagon Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/pr ...

  5. Codeforces Round #313 (Div. 2) C. Gerald's Hexagon

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #313 (Div. 1) A. Gerald's Hexagon 数学题

    A. Gerald's Hexagon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/p ...

  7. uva 317 - Hexagon(规律推导)

    题目连接:317 - Hexagon 题目大意:在一个19个六边形组成的图形上玩一个游戏,给出9个数字, 分成3组, 分别可以填在左上角, 上, 有上角,因为对于小六边形来说, 对边的数是相同的, 然 ...

  8. codeforces 559A(Gerald's Hexagon)

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Gera ...

  9. 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald&#39;s Hexagon

    [CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/C 题面: C. Gerald's Hexagon time limit per tes ...

随机推荐

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. Ubuntu上安装gtk2.0不能安装的问题,“下列的软件包有不能满足的依赖关系”

    zez@localhoss:~$ sudo apt-get install libgtk2.0-dev正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成 ...

  3. php __set() __get() __isset() __unset()四个方法的应用

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是,对属性的读取 和赋值操作是非常频繁的,因此在PHP5 中,预定义了两个函数“__get()”和“__set()”来获 取和赋值其 ...

  4. macbook air电池保养方法

    转自: http://bbs.feng.com/read-htm-tid-5819895.html http://www.zhihu.com/question/22628030

  5. 47. 数组中出现次数超过一半的数字[Number appears more than half times]

    [题目]:数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字. 例如长度为9的数组{1,2,3,2,2,2,5,4,2}中次数超过了数组长度的一半的数字为2,而长度为8的数组{1,2,3,2 ...

  6. 36.在字符串中删除特定的字符[Delete source from dest]

    [题目] 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”. ...

  7. 用数据表创建树_delphi教程

    数据库结构:字段 类型ID 整型 索引(无重复)name 文本father 整型 //tree初始化procedure TForm1.FormActivate(Sender: TObject);var ...

  8. Python多线程(2)——线程同步机制

    本文介绍Python中的线程同步对象,主要涉及 thread 和 threading 模块. threading 模块提供的线程同步原语包括:Lock.RLock.Condition.Event.Se ...

  9. mysql如何利用Navicat 导出和导入数据库

    MySql是我们经常用到的数据,无论是开发人员用来练习,还是小型私服游戏服务器,或者是个人软件使用,都十分方便.对于做一些个人辅助软件, 选择mysql数据库是个明智的选择,有一个好的工具更是事半功倍 ...

  10. javascript类的类比详解-大白话版

    转载请注明出处:水车 如果有误,还望指出,谢谢 -----------------正文分割线---------------------- 类:类太抽象,要想弄明白就该用现实的东西来类比 在我看来类就是 ...