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 ...
随机推荐
- How to install OpenResty
How to install OpenResty 15 January 2014, 6:18 am OpenResty, also called “ngx_openresty”, is a web ...
- android获取手机信息大全
IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...
- 利用zabbix监控某个目录大小
近期,因为JMS的消息堆积导致ApacheMQ频率故障(消息没有被消费掉,导致其数据库达到1.2G,JMS此时直接挂掉),很是郁闷!刚好自 己在研究zabbix.既然zabbix如此强大,那么它可以监 ...
- github pages 添加godaddy的dns解析
转自: http://andrewsturges.com/blog/jekyll/tutorial/2014/11/06/github-and-godaddy.html I own a custom ...
- 【云计算】docker registry v2简介
ubuntu docker方式部署docker registry v2 2016-03-03 17:36 by JackieHan, 4 阅读, 0 评论, 收藏, 编辑 生成自己签名的证书 生成签 ...
- order by 指定顺序 mysql
LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回字符串str第一次出现的子串SUBSTR的位置.第二个语法返回第一次出现在字符串str的子串SUBS ...
- js指定标签的id只能添加不能删除
<body> <form id="form1" runat="server"> <div> <input id=&qu ...
- iOS 和 Android 中的Alert
iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...
- 在SQLServer处理中的一些问题及解决方法 NEWSEQUENTIALID()
一.DBLINK性能问题select * from dbsource.dbname.dbo.table where guid in (select guid from tablechangelog w ...
- java\c程序的内存分配
JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该 ...