【BZOJ 2595】[Wc2008] 游览计划

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2595
数据生成器:http://paste.ubuntu.com/23070779/

斯坦纳树第一题! 撒花~ *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。
然而感觉斯坦纳树,撑死了算一类DP问题,为什么要叫算法呢QAQ
其实斯坦纳树,说白了就是一个状压DP?

题解的话,让我们召唤神犇:http://www.cnblogs.com/lidaxin/p/5299762.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define id(x,y) (((y)-1)*n+(x))
using namespace std;

const int MAXN = 19*19;
const int N = 2500;
const int INF = 0x3f3f3f3f;

int n,m,cnt,mat[MAXN],f[MAXN][N],num[MAXN],X,Y,vis[MAXN][N],pv,avl[MAXN];
queue<pair<int,int> > que;
pair<int,int> from[MAXN][N];

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

inline void rev(int w){
	Y = w / n + 1;
	X = w - (Y-1)*n;
	if (!X) X = n, Y--;
}

inline void update(int op, int nv, int pos, int w){
	if (f[pos][w] > nv) {
		f[pos][w] = nv;
		from[pos][w] = make_pair(op,w);
		if (!vis[pos][w]) vis[pos][w] = 1, que.push(make_pair(pos,w));
	}
}

inline void SPFA(){
	while (!que.empty()) {
		rev(que.front().first);
		int x=X, y=Y, w=que.front().second, pos=que.front().first; 
		vis[pos][w] = 0; que.pop();
		if (x > 1) update(pos,f[pos][w]+mat[id(x-1,y)],id(x-1,y),w);
		if (y > 1) update(pos,f[pos][w]+mat[id(x,y-1)],id(x,y-1),w);
		if (x < n) update(pos,f[pos][w]+mat[id(x+1,y)],id(x+1,y),w);
		if (y < m) update(pos,f[pos][w]+mat[id(x,y+1)],id(x,y+1),w);
	}
}

inline void Steiner_Tree(){
	for (int w=1,lim=1<<cnt;w<lim;w++) {
		for (int j=1;j<=m;j++) for (int i=1;i<=n;i++) {
			for (int k=w&(w-1);k;k=w&(k-1)) {
				int tmp = f[id(i,j)][k] + f[id(i,j)][w^k] - mat[id(i,j)];
				if (f[id(i,j)][w] > tmp) f[id(i,j)][w] = tmp, from[id(i,j)][w] = make_pair(id(i,j),k);		
			}
			if (f[id(i,j)][w] != INF) que.push(make_pair(id(i,j),w)), vis[id(i,j)][w] = 1;
		} SPFA();
	}
}

void Mark_Ans(pair<int,int> tmp){
	int pos=tmp.first, w = tmp.second; if (!pos) return; 
	rev(pos); int x = X, y = Y; avl[id(x,y)] = 1; 
	Mark_Ans(from[pos][w]); 
	if (from[pos][w].first == pos) Mark_Ans(make_pair(id(x,y),w-from[pos][w].second));
	
}

int main(){
	m = read(); n = read();
	for (int j=1;j<=m;j++) for (int i=1;i<=n;i++) {
		memset(f[id(i,j)],0x3f,sizeof(f[id(i,j)]));
		f[id(i,j)][0] = 0; mat[id(i,j)] = read(); 
		if (!mat[id(i,j)]) f[id(i,j)][1<<cnt++] = 0, pv = id(i,j);
	} 
	Steiner_Tree(); Mark_Ans(make_pair(pv,(1<<cnt)-1));
	printf("%d\n",f[pv][(1<<cnt)-1]);
	for (int j=1;j<=m;j++) {
		for (int i=1;i<=n;i++) 
			if (!mat[id(i,j)]) putchar('x');
			else if (avl[id(i,j)]) putchar('o');
			else putchar('_');
		putchar('\n');
	}
	return 0;
}

85 thoughts to “【BZOJ 2595】[Wc2008] 游览计划”

  1. I’m really enjoying the design and layout of your website.

    It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire
    out a designer to create your theme? Excellent work!

  2. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on a number
    of websites for about a year and am nervous about
    switching to another platform. I have heard very good things about blogengine.net.

    Is there a way I can transfer all my wordpress posts
    into it? Any help would be greatly appreciated!

  3. Hi there, i read your blog from time to time and i own a similar one and i was
    just curious if you get a lot of spam remarks?
    If so how do you protect against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any support is very much appreciated.

  4. Greetings I am so happy I found your web site, I really found you by error, while I was searching on Aol for something else, Regardless I am here now and would just like to say thank you
    for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have
    time to browse it all at the moment but I have bookmarked it and also included your
    RSS feeds, so when I have time I will be back to read
    much more, Please do keep up the awesome job.

  5. Hi there, just became aware of your blog through Google, and found that it
    is really informative. I am gonna watch out for brussels.

    I’ll be grateful if you continue this in future.

    Numerous people will be benefited from your writing.
    Cheers!

  6. Fascinating blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog stand out.
    Please let me know where you got your design. Thank you

  7. An outstanding share! I’ve just forwarded this onto a co-worker who has been conducting a little homework on this.
    And he in fact ordered me breakfast because I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending some time to talk about this matter here
    on your website.

  8. Hey there! This is my first comment here so I just wanted to give a quick shout out and tell
    you I truly enjoy reading through your posts.
    Can you recommend any other blogs/websites/forums that cover
    the same topics? Thanks for your time!

  9. Nice post. I was checking constantly this blog and I am impressed!

    Extremely useful info specially the last part 🙂 I care for such info a lot.
    I was seeking this certain information for a long time.
    Thank you and good luck.

  10. Hello there, There’s no doubt that your website
    could possibly be having browser compatibility problems.
    Whenever I look at your site in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues.
    I merely wanted to provide you with a quick heads up!
    Other than that, fantastic blog!

  11. Hi, I do believe this is an excellent website.
    I stumbledupon it 😉 I’m going to revisit yet again since i
    have book-marked it. Money and freedom is the greatest way
    to change, may you be rich and continue to guide
    other people.

  12. You made some decent points there. I looked on the internet to learn more about the issue and
    found most individuals will go along with your views on this website.
    plenty of fish natalielise

  13. Greetings I am so grateful I found your website,
    I really found you by accident, while I was browsing on Aol for something else, Regardless I
    am here now and would just like to say cheers for a remarkable post and a all round entertaining
    blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have bookmarked it and also
    included your RSS feeds, so when I have time I will be back to
    read a lot more, Please do keep up the great work. plenty of fish
    natalielise

  14. It’s really a nice and helpful piece of info. I’m
    glad that you shared this helpful information with us.
    Please stay us informed like this. Thank you for sharing.

  15. First of all I want to say fantastic blog! I had
    a quick question that I’d like to ask if you don’t mind.
    I was curious to find out how you center yourself and clear your mind
    prior to writing. I’ve had difficulty clearing my thoughts in getting my
    ideas out there. I truly do enjoy writing however it just seems like the first 10 to 15 minutes tend to be lost just trying to figure out
    how to begin. Any recommendations or hints? Thank you!
    natalielise pof

  16. Heya! I just wanted to ask if you ever have
    any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due
    to no back up. Do you have any methods to protect against hackers?

  17. Definitely believe that which you stated. Your favorite reason appeared to be at the internet the easiest factor
    to consider of. I say to you, I definitely
    get annoyed at the same time as people consider worries that they
    plainly don’t realize about. You managed to hit the nail upon the highest as smartly as defined
    out the whole thing without having side effect , other people can take a signal.

    Will probably be back to get more. Thanks

  18. Hey there! I’m at work surfing around your blog from my new iphone 3gs!
    Just wanted to say I love reading through your blog and look forward
    to all your posts! Keep up the excellent work!

  19. I’ve been surfing on-line more than three hours lately,
    but I by no means found any attention-grabbing article like yours.
    It’s pretty value enough for me. In my opinion, if all webmasters
    and bloggers made excellent content material as you probably did, the web will likely be a
    lot more useful than ever before.

  20. Unquestionably believe that which you stated. Your favorite justification seemed to be
    on the net the simplest thing to be aware of. I say to you,
    I certainly get irked while people consider worries that they just do not
    know about. You managed to hit the nail upon the top and
    also defined out the whole thing without having side-effects ,
    people can take a signal. Will probably be back to get more.
    Thanks

  21. I was more than happy to uncover this web site. I need
    to to thank you for ones time for this particularly wonderful read!!
    I definitely enjoyed every part of it and i also
    have you saved to fav to look at new things on your website.

  22. Heya i am for the first time here. I found this board and I find It really useful &
    it helped me out a lot. I hope to give something back and help
    others like you aided me.

  23. Good day! Would you mind if I share your blog with my facebook group?
    There’s a lot of people that I think would really
    enjoy your content. Please let me know. Thank you

  24. Thank you for the auspicious writeup. It in truth was a entertainment account it.
    Look complicated to more brought agreeable from you! However, how can we keep up a
    correspondence?

  25. Greetings from Los angeles! I’m bored at work so
    I decided to check out your blog on my iphone during lunch break.
    I love the knowledge you provide here and can’t wait to take a
    look when I get home. I’m amazed at how quick your blog loaded
    on my phone .. I’m not even using WIFI, just
    3G .. Anyways, very good blog!

  26. Just want to say your article is as surprising. The clearness in your post is just great
    and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please continue the enjoyable
    work.

  27. It’s a pity you don’t have a donate button! I’d most certainly donate to this excellent
    blog! I suppose for now i’ll settle for bookmarking and adding
    your RSS feed to my Google account. I look forward to fresh
    updates and will share this website with my Facebook group.
    Talk soon!

  28. I’ve been exploring for a little bit for any high quality articles or weblog posts
    on this sort of space . Exploring in Yahoo I eventually stumbled upon this website.
    Reading this info So i am happy to exhibit that I’ve an incredibly just right uncanny
    feeling I found out exactly what I needed.
    I such a lot for sure will make sure to don?t forget this web site and provides it a look regularly.

  29. Whats up this is kind of of off topic but I was wondering if
    blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  30. My brother recommended I may like this website. He used to
    be totally right. This publish truly made my day.
    You can not consider just how a lot time I had spent for this information! Thanks!

  31. Greetings from Los angeles! I’m bored at work so I decided to check out
    your blog on my iphone during lunch break. I really
    like the information you present here and can’t wait
    to take a look when I get home. I’m surprised at how fast your
    blog loaded on my mobile .. I’m not even using WIFI, just 3G ..

    Anyways, great site!

  32. Greetings! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly? My site looks weird when viewing from my apple iphone.
    I’m trying to find a template or plugin that might be able to fix this
    issue. If you have any recommendations, please share.
    Many thanks!

  33. I am really impressed with your writing skills as well as with the layout
    on your weblog. Is this a paid theme or did you customize it yourself?

    Anyway keep up the nice quality writing, it is rare to see a great blog like this one today.

  34. I absolutely love your blog.. Great colors & theme.
    Did you make this site yourself? Please reply back as I’m attempting to create my own site
    and want to find out where you got this from or what the theme is named.

    Appreciate it!

  35. Hello my loved one! I want to say that this article is awesome, nice written and come with approximately all significant
    infos. I would like to look extra posts like this .

  36. Hi, I think your site might be having browser compatibility issues.
    When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give
    you a quick heads up! Other then that, very good blog!

  37. Thank you for another informative website. The place else may just I am getting that kind of information written in such
    an ideal manner? I have a challenge that I’m just now running on, and I’ve been on the glance out for such info.

  38. Wow, awesome blog layout! How lengthy have you ever been blogging for?

    you make running a blog glance easy. The entire glance of your web site is wonderful, let alone the content!

  39. Great article! That is the kind of information that are supposed to be shared across the net.

    Shame on Google for now not positioning this put up upper!
    Come on over and talk over with my web site . Thanks =)

  40. Hey there! I simply would like to give you a huge thumbs up for your
    great info you have here on this post. I am coming back to your web site for more soon.

  41. Hello, Neat post. There is an issue along with your web site in web explorer,
    may check this? IE nonetheless is the market chief and
    a huge component of other folks will leave out your fantastic writing due to this problem.

  42. Hi! I’m at work surfing around your blog from my new iphone!

    Just wanted to say I love reading through your blog and look forward
    to all your posts! Keep up the excellent work!

  43. Awesome website you have here but I was curious if you knew of any
    forums that cover the same topics discussed here? I’d really like to
    be a part of online community where I can get opinions from
    other knowledgeable people that share the same interest.
    If you have any recommendations, please let me know. Kudos!

  44. Very good blog! Do you have any recommendations for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or
    go for a paid option? There are so many choices out there that I’m completely confused
    .. Any ideas? Thank you!

  45. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message home a little
    bit, but other than that, this is excellent blog.
    A great read. I will definitely be back.

  46. Hi there it’s me, I am also visiting this site on a regular
    basis, this website is actually fastidious and the
    users are in fact sharing good thoughts.

  47. When some one searches for his vital thing, therefore he/she desires to be available that in detail,
    therefore that thing is maintained over here.

  48. Does your website have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
    I’ve got some suggestions for your blog you
    might be interested in hearing. Either way, great blog and I look
    forward to seeing it improve over time.

Leave a Reply

Your email address will not be published. Required fields are marked *