hdu 3790 最短路徑問題(距離和費用)

2021-09-23 15:21:21 字數 827 閱讀 8165

給你n個點,m條無向邊,每條邊都有長度d和花費p,給你起點s終點t,要求輸出起點到終點的最短距離及其花費,如果最短距離有多條路線,則輸出花費最少的。

input

輸入n,m,點的編號是1~n,然後是m行,每行4個數 a,b,d,p,表示a和b之間有一條邊,且其長度為d,花費為p。最後一行是兩個數 s,t;起點s,終點。n和m為0時輸入結束。

(1題解:一開始用spfa做的一直超時,這一題的m比較大,n叫小,用dijkstra做。

#include#include #include#include #include #include using namespace std;

priority_queue>q;

struct nodeedge[200005];

int head[200005],ver[200006],nextt[200005],di[1005],co[1005];

int n,m,x,y,z,cc;

int tot;

bool v[1005];

void add(int x,int y,int z,int cc)

void dijkstra(int s)

else if(co[y]>co[x]+cos)

co[y]=co[x]+cos;

q.push(make_pair(-di[y],y));}}

} }

int main()

int s,t;

cin>>s>>t;

dijkstra(s);

cout<}

return 0;

}

hdu3790 最短路徑問題

wa了無數次,終於過了,還是dijkstra,只不過在距離,相同時,多判斷一下時間。注意重邊!include include include define m 0x7fffffff using namespace std struct p 2000 struct c 1020 1020 int vi...

HDU 3790 最短路徑問題

hdu 3790 最短路徑問題 單源最短路勁,更新路勁時要更新花費 include include include includeusing namespace std define inf 0x7fffffff int n,m int map 1005 1005 int cost 1005 100...

HDU 3790 最短路徑問題

include include include define nil 100000 struct node node 1001 初始化操作,把源點初始化為0,其他正無窮 param n 結點個數 param s 源點 int init shortpath source int n,int s nod...