BZOJ1401 : Hexagon
这题显然是一个最小斯坦纳树的模型,直接上模板即可,就是正六边形比较恶心。
#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的更多相关文章
- Gerald's Hexagon
Gerald's Hexagon Gerald got a very curious hexagon for his birthday. The boy found out that all the ...
- Codeforces Round #313 (Div. 1) A. Gerald's Hexagon
Gerald's Hexagon Problem's Link: http://codeforces.com/contest/559/problem/A Mean: 按顺时针顺序给出一个六边形的各边长 ...
- UVALive 6124 Hexagon Perplexagon 题解
http://vjudge.net/problem/viewProblem.action?id=37480 East Central Regional Contest Problem C: Hexag ...
- 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 ...
- 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 ...
- 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 ...
- uva 317 - Hexagon(规律推导)
题目连接:317 - Hexagon 题目大意:在一个19个六边形组成的图形上玩一个游戏,给出9个数字, 分成3组, 分别可以填在左上角, 上, 有上角,因为对于小六边形来说, 对边的数是相同的, 然 ...
- codeforces 559A(Gerald's Hexagon)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Gera ...
- 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/C 题面: C. Gerald's Hexagon time limit per tes ...
随机推荐
- index and polymorphic
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations class CreateStars < ...
- 关于ruby重构的过程中去除不必要的format
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) #这段话可以由下面的话替代56 respond_to do |format|57 ...
- C#开发实例 键盘篇
键盘的操作控制: 键盘和鼠标一样是重要输入设备的一部分.开发过程中,会涉及到很多的键盘操作控制. 2.1获取键盘信息 ①获取组合键 Windows中有很多默认的组合键,如Ctrl+v,Ctrl+A.本 ...
- Coursera台大机器学习课程笔记14 -- Validation
这节课是接着上节的正则化课程的,目的也是为了防止overfitting. 第一小节讲了模型的选择,前面讲了很多模型,那么如何做出正确的选择呢?我们的目标是选择最小的Eout目标函数.首先应避免视觉化选 ...
- Python爬虫经验
有时候读取同一个url,服务器可能会返回不同的response,并不是爬虫程序代码的问题,而是服务器的问题,初次试验request时,最好 把response文本保存在一个txt文件当中,以便后续的比 ...
- nginx lua处理图片
user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ...
- 【OpenStack】OpenStack系列7之Nova详解
源码下载.安装 参考: https://github.com/yongluo2013/osf-openstack-training/blob/master/installation/openstack ...
- C++文件输入输出
#include <iostream> //有些系统里面可以省略此头文件 #include <fstream> #include <string> int main ...
- C++ virtual descructor
[代码1] C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 ...
- C++基础知识面试精选100题系列(1-10题)[C++ basics]
[原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...