# 【BZOJ 4356】[CEOI2014] Wall

### 解题报告

bzoj_4356_solution

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int L = 400 + 9;
const int N = L * L * 4;
const int M = N * 4;
const LL INF = 1e17;

int cx[L][L],cy[L][L],intree[L][L];
int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};
bool del[L][L][4],vis[L][L],done[N],bst[L][L][4];
LL dis[N];
priority_queue<pair<LL, int> > que;

char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

inline void Add_Edge(int u, int v, int c) {
to[++E] = v; nxt[E] = head[u]; head[u] = E; cost[E] = c;
to[++E] = u; nxt[E] = head[v]; head[v] = E; cost[E] = c;
}

inline int id(int x, int y, int t = 0) {
if (!t) return x + (y - 1) * (n + 1);
else return (x - 1 + (y - 1) * (n + 1) << 2) + t;
}

inline void Dijkstra(int s) {
memset(done, 0, sizeof(done));
fill(dis, dis+N, INF); dis[s] = 0;
que.push(make_pair(0, s));
for (int w;!que.empty();) {
w = que.top().second; que.pop();
if (done[w]) continue;
done[w] = 1;
if (dis[to[i]] > dis[w] + cost[i]) {
dis[to[i]] = dis[w] + cost[i];
que.push(make_pair(-dis[to[i]], to[i]));
}
}
}
}

bool mark(int x, int y, int f) {
if (intree[x][y]) return ~intree[x][y]?1:0;
bool tag = vis[x][y];
if (dis[w] + cost[i] == dis[to[i]] && to[i] != f) {
for (int j=1,nx,ny;j<=4;j++) {
nx = x + dx[j]; ny = y + dy[j];
if (id(nx, ny) == to[i]) {
if (mark(nx, ny, w)) {
bst[x][y][j] = tag = 1;
bst[nx][ny][j+2>4?j-2:j+2] = 1;
}
break;
}
}
}
}
intree[x][y] = tag?1:-1;
return tag;
}

int main() {
del[1][1][2] = del[1][1][4] = 1;
for (int j=1;j<=m;j++) {
for (int i=1;i<=n;i++) {
vis[i][j] = 1;
del[i][j][4] = 1;
del[i+1][j][3] = 1;
del[i][j+1][1] = 1;
del[i+1][j+1][2] = 1;
}
}
}
for (int j=1,tmp;j<=m;j++) {
for (int i=1;i<=n+1;i++) {
}
}
for (int j=1,tmp;j<=m+1;j++) {
for (int i=1;i<=n;i++) {
}
}
Dijkstra(id(1,1));
mark(1, 1, id(1, 1));
for (int j=1;j<=m+1;j++) {
for (int i=1;i<=n+1;i++) {
if (!del[i][j][1]) {
if (!del[i][j][4] && !bst[i][j][1]) Add_Edge(id(i, j, 1), id(i, j, 4), 0);
if (i <= n && !del[i+1][j][2]) Add_Edge(id(i, j, 1), id(i+1, j, 2), cx[i][j]);
}
if (!del[i][j][2]) {
if (!del[i][j][1] && !bst[i][j][4]) Add_Edge(id(i, j, 2), id(i, j, 1), 0);
if (!del[i][j][3] && !bst[i][j][3]) Add_Edge(id(i, j, 2), id(i, j, 3), 0);
}
if (!del[i][j][3]) {
if (!del[i][j][4] && !bst[i][j][2]) Add_Edge(id(i, j, 3), id(i, j, 4), 0);
if (j <= m && !del[i][j+1][2]) Add_Edge(id(i, j, 3), id(i, j+1, 2), cy[i][j]);
}
if (!del[i][j][4]) {
if (i <= n && !del[i+1][j][3]) Add_Edge(id(i, j, 4), id(i+1, j, 3), cx[i][j]);
if (j <= m && !del[i][j+1][1]) Add_Edge(id(i, j, 4), id(i, j+1, 1), cy[i][j]);
}
}
}
Dijkstra(id(1,1,3));
printf("%lld\n",dis[id(1,1,1)]);
return 0;
}


## 27 thoughts to “【BZOJ 4356】[CEOI2014] Wall”

1. Hi there! Quick question that’s entirely off topic.
Do you know how to make your site mobile friendly? My weblog
looks weird when browsing from my iphone4. I’m trying to find a
theme or plugin that might be able to correct this problem.
If you have any recommendations, please share. Cheers!

2. What i do not understood is if truth be told how you are now not actually
much more well-appreciated than you might be now.
You are so intelligent. You already know therefore significantly in relation to this subject, made me
in my opinion imagine it from a lot of numerous angles.
Its like men and women are not interested except it’s something to do
with Woman gaga! Your personal stuffs outstanding. All the time care for it
up!

3. Hey There. I found your blog using msn. This is a really well written article.
I will be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I’ll certainly return.

4. This is very interesting, You’re a very skilled blogger.
I’ve joined your rss feed and look forward to in the hunt for
shared your web site in my social networks

5. We are a group of volunteers and opening a brand
new scheme in our community. Your web site offered us with helpful information to work on. You have
performed a formidable task and our whole group might be thankful
to you.

6. Hello, I want to subscribe for this website to take most recent updates, therefore where can i do it please assist.

7. Hello to all, because I am truly keen of reading this web site’s post to be updated
daily. It consists of fastidious material.

8. I’m not that much of a internet reader to be honest but your blogs really nice, keep
come back later. All the best

9. Hey there! This post could not be written any better!
Reading this post reminds me of my good old room mate!

Fairly certain he will have a good read. Many thanks for sharing!

10. Thanks for the marvelous posting! I certainly enjoyed reading it, you may be a great author.
I will make sure to bookmark your blog and will often come back someday.
I want to encourage you to continue your great work, have a nice day!

11. Good answers in return of this issue with genuine arguments
and telling the whole thing concerning that.

12. Hey there! This post could not be written any better!
Reading through this post reminds me of my previous room mate!

Fairly certain he will have a good read. Thank you for sharing!

13. I enjoy what you guys tend to be up too. Such clever work and coverage!
Keep up the good works guys I’ve incorporated you guys to my
personal blogroll.

14. Hello! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks

15. Pretty! This was an incredibly wonderful article. Many thanks for providing
these details.

16. It’s wonderful that you are getting ideas from this piece of writing
as well as from our discussion made here.

17. Thanks a lot for sharing this with all of us you really
realize what you’re speaking about! Bookmarked.

We will have a hyperlink change contract between us

18. I really like it whenever people get together and share opinions.
Great site, keep it up!

19. Wonderful web site. A lot of useful information here. I’m sending it to some
pals ans additionally sharing in delicious. And naturally, thanks on your sweat!

20. Superb website you have here but I was wanting to
know if you knew of any discussion boards that cover the same topics
discussed here? I’d really love to be a part of community where I
can get feed-back from other experienced people that
share the same interest. If you have any recommendations, please let me know.
Kudos!

21. I really like what you guys are usually up too. Such clever
work and exposure! Keep up the very good works guys I’ve incorporated you guys
to our blogroll.

22. Hi there, after reading this amazing piece of writing i am too glad to share my experience
here with colleagues.

23. Hey there! I just wanted to ask if you ever have any
problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of
hard work due to no backup. Do you have any methods to protect against hackers?

24. Asking questions are really good thing if you are not understanding anything completely, however this piece of writing presents good understanding even.

25. It’s an awesome post in favor of all the internet
people; they will take benefit from it I am sure.

26. Definitely, what a magnificent site and illuminating posts, I surely will bookmark your site.Have an awsome day!