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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

洛谷 P2384 最短路题解

發(fā)布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 P2384 最短路题解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目背景

狗哥做爛了最短路,突然機智的考了Bosh一道,沒想到把Bosh考住了...你能幫Bosh解決嗎?

他會給你100000000000000000000000000000000000%10金幣w

題目描述

給定n個點的帶權(quán)有向圖,求從1到n的路徑中邊權(quán)之積最小的簡單路徑。

輸入格式

第一行讀入兩個整數(shù)n,m,表示共n個點m條邊。 接下來m行,每行三個正整數(shù)x,y,z,表示點x到點y有一條邊權(quán)為z的邊。

輸出格式

輸出僅包括一行,記為所求路徑的邊權(quán)之積,由于答案可能很大,因此狗哥仁慈地讓你輸出它模9987的余數(shù)即可。

廢話當然是一個數(shù)了w

//謝fyszzhouzj指正w

對于20%的數(shù)據(jù),n<=10。

對于100%的數(shù)據(jù),n<=1000,m<=1000000。邊權(quán)不超過10000。

輸入輸出樣例

輸入 #1復制 3 3 1 2 3 2 3 3 1 3 10 輸出 #1復制 9

說明/提示

好好看一看再寫喲w


?

題解

這道題目和通常的最短路的最大差別在于它計算的不是邊權(quán)之和,二是邊權(quán)乘積。代碼如下。里面只有一個小坑就是有兩個測試例的數(shù)據(jù)中邊長可能為9987,所以如果直接將邊的成績?nèi)ツ?987會產(chǎn)生0,從而出現(xiàn)錯誤結(jié)果。程序中對這種情況進行了特判,如果出現(xiàn)模后的結(jié)果為0,則將模后的結(jié)果指定為9987。

1 #include <iostream> 2 #include <queue> 3 #include <string.h> 4 5 using namespace std; 6 7 struct edge 8 { 9 int zhongdian, changdu; 10 int next = 0; 11 }; 12 13 int first[2333]; 14 15 edge ed[200005]; 16 17 int n, m, en; 18 19 void add_edge( int s, int e, int d ) 20 { 21 en++; 22 ed[en].next = first[s]; 23 first[s] = en; 24 ed[en].zhongdian = e; 25 ed[en].changdu = d; 26 } 27 28 29 const int MAXN = 100010; 30 const int INF = 0x3f3f3f3f; 31 int dist[MAXN]; 32 33 bool use[MAXN]; 34 35 struct rec 36 { 37 int p, dist; 38 39 rec() 40 { 41 } 42 rec( int a, int b ) 43 44 { 45 p = a, dist = b; 46 } 47 }; 48 49 bool operator < (const rec &a, const rec &b) 50 51 { 52 return(a.dist > b.dist); 53 } 54 55 priority_queue<rec> heap; 56 57 void dijkstra_heap() 58 59 { 60 memset( dist, 0x3f3f, sizeof(dist) ); 61 62 dist[1] = 1; 63 for ( int a = 1; a <= n; a++ ) 64 { 65 heap.push( rec( a, dist[a] ) ); 66 } 67 for ( int a = 1; a <= n; a++ ) 68 { 69 while ( use[heap.top().p] ) 70 { 71 heap.pop(); 72 } 73 rec now = heap.top(); 74 heap.pop(); 75 int p = now.p; 76 use[p] = true; 77 for ( int i = first[p]; i; i = ed[i].next ) 78 { 79 if ( dist[p] * ed[i].changdu < dist[ed[i].zhongdian] ) 80 81 { 82 dist[ed[i].zhongdian] = (dist[p] * ed[i].changdu) % 9987; 83 if ( dist[ed[i].zhongdian] == 0 ) 84 { 85 dist[ed[i].zhongdian] = 9987; 86 } 87 heap.push( rec( ed[i].zhongdian, dist[ed[i].zhongdian] ) ); 88 } 89 } 90 } 91 } 92 93 94 int main() 95 { 96 cin >> n >> m; 97 for ( int a = 1; a <= m; a++ ) 98 { 99 int s, e, d; 100 cin >> s >> e >> d; 101 add_edge( s, e, d ); 102 add_edge( e, s, d ); 103 } 104 dijkstra_heap(); 105 cout << dist[n] << endl; 106 return(0); 107 }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zealsoft/p/11530015.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的洛谷 P2384 最短路题解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。