题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2069
数据传送门:http://oi.cyo.ng/wp-content/uploads/2016/10/2069.rar
这题说两个做法:
1)最短路+奇怪的姿势,时间复杂度O(nlogn)
2)关键点间的最短路,时间复杂度O(nlog^2n)
先说第一种方式:
从1开始跑最短路,每个点记录最短路+次短路,保证路径上第二个点不同(1算第一个点)
然后O(n)扫描每一个点,使用最短路+次短路更新答案即可
这种方法虽然时间复杂度较优,但不是很通用
接下来我们说一说关键点间的最短路:
考虑将答案路径与1相连的两条边删掉
路径变为与1相连的点间的最短路径
于是将与1相邻的点设为关键点,跑关键点间的最短路即可
接下来说一说怎么跑关键点间的最短路:
考虑拆二进制,枚举二进制的每一位。
根据这一位是否为1,将关键点分为两个点集
然后一个连超级源,另一个连超级汇即可
时间复杂度O(nlog^2(n))
其实这才是这篇题解的重点
这里是二进制拆分+Dijkatra的代码,请随意食用~
#include<bits/stdc++.h> #define LL long long using namespace std; const int N = 50000+9; const int M = 300000+9; const int INF = 1e9; int head[N],dis[N],t1[N],t2[N],t3[N],head_tmp[N],tot,TT,S,T,vout=INF,n,m,nxt[M],to[M],cost[M]; priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que; bool vis[N]; inline int read(){ char c=getchar(); int ret=0,f=1; 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 c1, int c2) { to[++TT] = v; nxt[TT] = head[u]; head[u] = TT; cost[TT] = c1; to[++TT] = u; nxt[TT] = head[v]; head[v] = TT; cost[TT] = c2; } inline int Dijkstra() { memset(dis,60,sizeof(dis)); memset(vis,0,sizeof(vis)); que.push(make_pair(0,S)); dis[S] = 0; vis[1] = 1; while (!que.empty()) { int w = que.top().second; que.pop(); if (vis[w]) continue; else vis[w] = 1; for (int i=head[w];i;i=nxt[i]) { if (dis[to[i]] > dis[w] + cost[i] && !vis[to[i]]) { dis[to[i]] = dis[w] + cost[i]; que.push(make_pair(dis[to[i]], to[i])); } } } return dis[T]; } int main(){ n = read(); m = read(); for (int i=1,a,b,c,d;i<=m;i++) { a = read(); b = read(); c = read(); d = read(); Add_Edge(a, b, c, d); if (a == 1) { t1[++tot] = b; t2[tot] = c; t3[tot] = d; } else if (b == 1) { t1[++tot] = a; t2[tot] = d; t3[tot] = c; } } S = N - 1; T = N - 2; int w = 0, TMP = n, ori = TT; while (TMP) w++, TMP >>= 1; memcpy(head_tmp,head,sizeof(head)); for (int k=0,cur=1;k<=w;k++,cur<<=1) { TT = ori; memcpy(head,head_tmp,sizeof(head)); for (int i=1;i<=tot;i++) { if (t1[i]&cur) Add_Edge(S,t1[i],t2[i],INF); else Add_Edge(t1[i],T,t3[i],INF); } vout = min(Dijkstra(), vout); TT = ori; memcpy(head,head_tmp,sizeof(head)); for (int i=1;i<=tot;i++) { if (t1[i]&cur) Add_Edge(t1[i],T,t3[i],INF); else Add_Edge(S,t1[i],t2[i],INF); } vout = min(Dijkstra(), vout); } printf("%d\n",vout); return 0; }
Genuinely no matter if someone doesn’t know afterward its up to other
people that they will assist, so here it occurs.
It’s very effortless to find out any matter on web as compared to books, as I found this post at
this web site.
I have been surfing online more than three hours lately,
yet I by no means found any interesting article like yours.
It is beautiful worth enough for me. In my view, if all web owners and bloggers made excellent content as you probably did,
the net shall be much more helpful than ever before.
Good info. Lucky me I recently found your blog by accident (stumbleupon).
I’ve book marked it for later!
Hi there, yup this piece of writing is actually pleasant and I have learned lot of things
from it regarding blogging. thanks.
Why users still make use of to read news papers when in this technological globe all is existing on net?
My brother suggested I might like this web site. He was entirely right.
This post actually made my day. You can not imagine simply how much
time I had spent for this info! Thanks!
I’m really loving the theme/design of your web site. Do
you ever run into any browser compatibility issues? A couple of my blog audience have complained about my site not working correctly in Explorer
but looks great in Chrome. Do you have any tips to
help fix this problem?
Hello there, just became aware of your blog
through Google, and found that it is truly informative.
I am going to watch out for brussels. I’ll be grateful if you continue this in future.
Numerous people will be benefited from your writing.
Cheers!
Link exchange is nothing else except it is just placing
the other person’s webpage link on your page at appropriate place and
other person will also do same in favor of you.
You have made some really good points there. I checked
on the net to learn more about the issue and found most
people will go along with your views on this web site.
Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
Wow that was unusual. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again. Anyways, just
wanted to say fantastic blog!
I have read so many articles concerning the blogger lovers
but this piece of writing is in fact a pleasant piece of writing,
keep it up.
I’ve been surfing online more than 3 hours nowadays, but I never discovered any interesting article like
yours. It’s lovely value enough for me. In my view, if
all webmasters and bloggers made excellent content material as you probably did,
the web can be much more helpful than ever before.
What a stuff of un-ambiguity and preserveness of valuable familiarity concerning unexpected emotions.
Having read this I believed it was really
enlightening. I appreciate you finding the time and effort to
put this information together. I once again find myself personally spending a lot
of time both reading and leaving comments. But so what, it was still worthwhile!
Awesome! Its in fact awesome post, I have got
much clear idea regarding from this piece of writing.
I believe this is among the most important info for me.
And i am glad reading your article. But wanna commentary on few common issues,
The site style is ideal, the articles is in reality nice
: D. Just right job, cheers natalielise pof
I could not resist commenting. Very well written! natalielise pof
I have been surfing online more than 2 hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the internet will
be a lot more useful than ever before.
Its not my first time to visit this web page, i am browsing this website dailly and get nice facts from here all the time.
Way cool! Some very valid points! I appreciate you writing this post and also the rest of the website is very good.
Thank you, I’ve just been looking for info about this topic for a while and yours is the
greatest I have came upon so far. However, what about the bottom line?
Are you certain concerning the source? pof natalielise
Hi! This is kind of off topic but I need some guidance from
an established blog. Is it very hard to set up your own blog?
I’m not very techincal but I can figure things
out pretty quick. I’m thinking about making my own but I’m not sure where to begin. Do you have any tips
or suggestions? Thank you
Hi there friends, good piece of writing and nice urging commented here, I am
really enjoying by these.
It’s truly a nice and useful piece of information. I’m happy that you shared this useful
info with us. Please stay us informed like this.
Thanks for sharing.
Attractive section of content. I just stumbled upon your weblog
and in accession capital to assert that I get in fact enjoyed account your blog posts.
Anyway I’ll be subscribing to your augment and even I achievement you access consistently quickly.
I get pleasure from, cause I discovered exactly what I used to be taking a look for.
You have ended my 4 day long hunt! God Bless you man. Have a nice day.
Bye
Amazing! Its genuinely amazing paragraph, I have
got much clear idea concerning from this paragraph.
I feel that is one of the so much significant info
for me. And i am glad reading your article.
However want to statement on some general things, The web site style is great, the articles is in point of fact great : D.
Excellent process, cheers
Piece of writing writing is also a excitement, if you be acquainted with after that you can write or else it is complicated to write.
great post, very informative. I’m wondering why the other specialists of this sector don’t understand this.
You should continue your writing. I’m sure, you have
a huge readers’ base already!
I don’t even know how I ended up here, but I thought this
post was good. I don’t know who you are but certainly you are going to a famous blogger if you
are not already 😉 Cheers!
Yes! Finally something about quest bars cheap.
For newest news you have to visit the web and on internet I found this
website as a most excellent website for hottest updates.
Excellent blog here! Also your website loads up fast!
What web host are you using? Can I get your affiliate link to
your host? I wish my web site loaded up as quickly as yours lol
Marvelous, what a web site it is! This weblog gives useful facts
to us, keep it up.
I do believe all of the concepts you’ve offered in your post.
They’re really convincing and can certainly work. Nonetheless, the posts are very short
for beginners. Could you please extend them a little from
next time? Thank you for the post.
Spot on with this write-up, I actually believe this website needs much more attention. I’ll probably be
back again to see more, thanks for the info!
I do not even know how I ended up here, but I thought this
post was great. I do not know who you are but definitely you are
going to a famous blogger if you aren’t already 😉 Cheers!
I am really loving the theme/design of your weblog.
Do you ever run into any internet browser compatibility problems?
A few of my blog readers have complained about my website not
working correctly in Explorer but looks great in Opera. Do you have any
tips to help fix this problem?
Wonderful web site. Plenty of helpful info here. I’m sending it to several buddies ans also sharing in delicious.
And of course, thanks for your sweat!
Pretty component of content. I just stumbled upon your web site and in accession capital to say that I get in fact
loved account your blog posts. Anyway I will be subscribing in your augment and
even I success you get right of entry to persistently quickly.
What’s up, everything is going well here and ofcourse every one
is sharing facts, that’s truly good, keep up writing.
I enjoy what you guys are usually up too. Such clever work and
reporting! Keep up the good works guys I’ve incorporated you guys
to blogroll.
Its such as you read my thoughts! You seem to understand a lot approximately this, such as
you wrote the e-book in it or something. I think that you can do
with a few percent to pressure the message home a
bit, however instead of that, this is great blog.
A great read. I’ll certainly be back.
Paragraph writing is also a excitement, if you be familiar with afterward you can write or else
it is difficult to write.
Hi! I’m at work surfing around your blog from my new iphone 4!
Just wanted to say I love reading your blog and look forward to all your posts!
Carry on the great work!
Now I am going away to do my breakfast, afterward having my breakfast coming yet again to read more news.
I’m gone to say to my little brother, that he should also pay a quick visit this blog on regular basis to get updated
from hottest gossip.
Woah! I’m really enjoying the template/theme of this website.
It’s simple, yet effective. A lot of times it’s challenging
to get that “perfect balance” between user friendliness
and visual appeal. I must say that you’ve done a fantastic job with
this. Additionally, the blog loads super fast for me on Opera.
Exceptional Blog!
Write more, thats all I have to say. Literally, it seems as though you relied on the video
to make your point. You definitely know what youre talking
about, why waste your intelligence on just posting videos to
your site when you could be giving us something informative to read?
I enjoy reading a post that will make people
think. Also, thanks for allowing for me to comment!
I think the admin of this site is really working hard in support of
his web site, as here every stuff is quality based information.
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 concerned about switching to another
platform. I have heard fantastic things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any kind of help would be really appreciated!
I am not sure the place you’re getting your information, but good
topic. I must spend some time finding out more or
understanding more. Thank you for excellent info
I used to be in search of this info for my mission.
I just like the valuable information you supply for your articles.
I’ll bookmark your weblog and check again here frequently.
I am fairly sure I’ll be informed lots of new stuff proper right here!
Best of luck for the next!
Hi there! I know this is somewhat off topic but I was wondering if you knew where I could
locate a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding
one? Thanks a lot!
Usually I do not read article on blogs, however I would like to say that this write-up very compelled me to check out and do so!
Your writing style has been surprised me. Thank you, quite
nice article.
I always used to read paragraph in news papers but now as I am a user of internet therefore from now I am using net for
content, thanks to web.
My partner and I absolutely love your blog and find many of your post’s to be what
precisely I’m looking for. Does one offer guest writers to write content available for you?
I wouldn’t mind composing a post or elaborating on most of the subjects you write with regards to here.
Again, awesome weblog!
It’s going to be ending of mine day, however before ending I am reading this enormous article to improve
my know-how.
I enjoy what you guys are up too. This kind of clever work and coverage! Keep up the great works guys I’ve you guys to my blogroll.
I like the valuable info you provide in your articles.
I will bookmark your weblog and check again here regularly.
I am quite sure I’ll learn lots of new stuff right here!
Good luck for the next!
It’s very trouble-free to find out any matter on net as compared to textbooks, as
I found this paragraph at this website.
Everyone loves what you guys are usually up too. This kind of clever work and reporting!
Keep up the awesome works guys I’ve added you guys to blogroll.
Pretty! This was an incredibly wonderful post. Thank you for supplying these details.
My brother suggested I might like this website.
He used to be entirely right. This publish actually made my day.
You can not imagine just how a lot time I had spent for this information! Thanks!
Excellent, what a web site it is! This web site gives useful
facts to us, keep it up.
I do not even understand how I ended up right here, however I believed this publish was once good.
I don’t know who you’re however certainly you’re going
to a famous blogger for those who are not already.
Cheers!
Magnificent website. Plenty of helpful information here.
I am sending it to some friends ans additionally sharing in delicious.
And certainly, thank you on your effort!
It’s going to be ending of mine day, however
before ending I am reading this great paragraph to increase my
know-how.
Good article! We will be linking to this particularly great article
on our website. Keep up the good writing.
Its wonderful as your other content : D, appreciate it for putting up.
Enjoyed every bit of your blog post.Really looking forward to read more. Cool.