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

#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. STL迭代器笔记

    STL迭代器简介 标准模板库(The Standard Template Library, STL)定义了五种迭代器.下面的图表画出了这几种: input         output \       ...

  2. Linux运维相关目录

  3. Java for LeetCode 153 Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. codeforces A. Difference Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2 ...

  5. hdu 1272 小希的迷宫 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 第二条并查集,和畅通工程的解法类似.判断小希的迷宫不符合条件,即有回路.我的做法是,在合并两个集 ...

  6. 一个程序中关于多个osgGA::GUIEventHandler同时存在的问题

    平时使用GUIEventHandler不太注意handle()函数的返回值,觉得返回true或者false都无所谓,其实不然. 我遇到的问题是程序中一个节点添加了GUIEventHandler对象pi ...

  7. Ruby备忘

  8. windows下同时安装python2与python3

    由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...

  9. C# Window Form解决播放amr格式音乐问题

    最近搞一个项目,需要获取微信端语音文件,下载之后发现是AMR格式的录音文件,这下把我搞晕了,C#中的4种播放模式不支持播放AMR,想到都觉得头痛,如何是好?最后找到的方案,其实也简单:windows ...

  10. linux tricks 之 bitmap分析.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...