日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU4889 Scary Path Finding Algorithm

發布時間:2023/12/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU4889 Scary Path Finding Algorithm 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Fackyyj loves the challenge phase in TwosigmaCrap(TC). One day, he meet a task asking him to find shortest path from vertex 1 to vertex n, in a graph with at most n vertices and m edges. (1 ≤ n ≤ 100,0 ≤ m ≤ n(n-1))?

Fackyyj solved this problem at first glance, after that he opened someone's submission, spotted the following code:?

1 long long spfa_slf() { 2 int n,m; 3 cin >> n >> m; 4 5 vector<pair<int,int> > edges111111; 6 for(int i = 0;i < m;i++) { 7 int x,y,w; 8 cin >> x >> y >> w; 9 edgesxx.push_back(make_pair(y,w)); 10 } 11 12 deque<int> q; 13 vector<long long> dist(n+1, ~0ULL>>1); 14 vector<bool> inQueue(n+1, false); 15 dist11 = 0; q.push_back(1); inQueue11 = true; 16 17 int doge = 0; 18 while(!q.empty()) { 19 int x = q.front(); q.pop_front(); 20 if(doge++ > C) { 21 puts("doge"); 22 return 233; 23 } 24 for(vector<pair<int,int> >::iterator it = edgesxx.begin(); 25 it != edgesxx.end();++it) { 26 int y = it->first; 27 int w = it->second; 28 if(distyy > distxx + w) { 29 distyy = distxx + w; 30 if(!inQueueyy) { 31 inQueueyy = true; 32 if(!q.empty() && distyy > distq.front()q.front()) 33 q.push_back(y); 34 else 35 q.push_front(y); 36 } 37 } 38 } 39 inQueuexx = false; 40 } 41 return distnn; 42 }

?


Fackyyj's face lit up with an evil smile. He immediately clicked button "Challenge!", but due to a hard disk failure, all of his test case generators were lost! Fackyyj had no interest on recreating his precise generators, so he asked you to write one. The generator should be able to generate a test case with at most 100 vertices, and it must be able to fail the above code, i.e. let the above code print "doge". It should?NOT?contain any negative-cost loop.?

 For those guys who doesn't know C++, Fackyyj explain the general idea of the above algorithm by the following psuedo-code:?

InputInput contains several test cases, please process till EOF.?
?For each test case, there will be a single line containing an integer C. It is the constant C in the above code. (C <= 23333333)OutputFor each test case, on the first line, print two integers, n and m, indicating the number of vertices and the number of edges of your graph. Next m lines, on each line print?x?y?w, means there is a road from x to y, cost w.?
1 ≤ n ≤ 100,0 ≤ m ≤ n(n-1),|w| < 2?31. Note that your output shouldn't contain any negative-cost loop.Sample Input

1

Sample Output

4 3 1 2 1 2 3 1 3 4 1

?

圖論 ?愉悅腦洞題 卡SPFA

給了一個SPFA的SLF優化程序,讓你把它卡掉。

這個SLF的機制大概是每次更新完,如果被更新的點dis比隊頭dis小,就把它插到隊頭。

根據這個性質,只要造出數據讓它多出許多遍重復的更新即可

http://blog.csdn.net/u012221059/article/details/38336633

↑這里有張圖可以很直觀地說明問題。可以發現,隨著三角數量的增長,復雜度成指數級上升。

?

莫慌,不加這個鬼畜優化的普通SPFA,復雜度上界還是$O(NM)$的

?

注釋掉的部分可以卡出一定的效果,但是卡不到指數級的樣子。

注意輸出順序也很關鍵。

那么問題來了,我為什么要花時間寫這么道沒意義的破題?

1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 int main(){ 10 // freopen("in.txt","w",stdout); 11 int i,j,C; 12 while(scanf("%d",&C)!=EOF){ 13 int n=61,m=90; 14 // int n=99,m=98/2*3; 15 printf("%d %d\n",n,m); 16 /* for(i=3;i<=n;i+=2){ 17 printf("%d %d %d\n",i-2,i,-(1<<(i-1))); 18 } 19 for(i=1;i<=n-2;i+=2){ 20 printf("%d %d %d\n",i,i+1,0); 21 } 22 for(i=2;i<=n;i+=2){ 23 printf("%d %d %d\n",i,i+1,-(1<<(i-2))); 24 }*/ 25 for(i=0;i<30;i++) 26 printf("%d %d %d\n",i*2+1,i*2+2,0); 27 for(i=0;i<30;i++) 28 printf("%d %d %d\n",i*2+2,i*2+3,-(1<<(30-i))); 29 for(i=0;i<30;i++) 30 printf("%d %d %d\n",i*2+1,i*2+3,-(1<<(30-i-1))); 31 } 32 return 0; 33 }

?

轉載于:https://www.cnblogs.com/SilverNebula/p/6710852.html

總結

以上是生活随笔為你收集整理的HDU4889 Scary Path Finding Algorithm的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。