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 ...
随机推荐
- Inheritance
Often, classes will have shared characteristics with other classes. Rewriting the same methods for e ...
- 能用Shell就别编程-海量文本型数据的处理
对于txt文本类数据,优先采用shell脚本,实在不行才用Python,Java,MySQL 1) Shell命令行或脚本的处理速度极快,比Java快得多. 2) Shell代码量少,几个命令就能完成 ...
- chrome断点续传功能
刚好找到了一个临时的解决方法,chrome其实已经内部实现了断点续传功能,不过应该还没完善,所以要自己打开.方法:用chrome在地址栏输入chrome://flags用搜索找到resumption( ...
- .Net Attribute特性
1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标 ...
- 使用WebDriver遇到的那些坑(转)
http://www.huangbowen.net/blog/2013/06/25/practice-of-webdriver/ 在做web项目的自动化端到端测试时主要使用的是Selenium Web ...
- XPath 定位----光荣之路
被测试网页的HMTL代码 <html> <body> <div id="div1"> <input name="div1inpu ...
- canvas实践小实例一 —— 画板工具
前面讲了一部分的canvasAPI的基础知识,光看API的介绍确实是很无趣乏味,需要一点可以激发内心的激情的东西来激励自己来学习,于是就了伴随canvasAPI学习的小实例,这样通过API的知识,结合 ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- springMVC获取file,几种转换
//从前台通过name值获取file MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)reque ...
- Netbeans快捷键
一.常用快捷键:1.在文件中查找指定内容 Ctrl+F2.在文件中替换指定内容 Ctrl+H3.在整个项目中查找指定内容 Ctrl+Shift+f4.自动复制整行代码 Ctrl+Shift+上/下方向 ...