USACO Milk Routing /// 優先隊列廣搜
阿新 • • 發佈:2019-03-17
its while routing onclick lap name def clear alt
題目大意:
在n個點 m條邊的無向圖中 需要運送X單位牛奶
每條邊有隱患L和容量C 則這條邊上花費時間為 L+X/C
求從點1到點n的最小花費
優先隊列維護 L+X/C 最小 廣搜到點n
#include <bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f #define mem(i,j) memset(i,j,sizeof(i)) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) constView Codeint N=2000+5; const int mod=1e9+7; int n,M; LL X; struct NODE { int u,v; LL L,C; bool operator <(const NODE& p) const { return (double)L+(double)X/C > (double)p.L+(double)X/p.C; } }; vector<NODE>G[505]; int main() { while(~scanf("%d%d%lld",&n,&M,&X)) { inc(i,1,n) G[i].clear(); inc(i,1,M) { int u,v; LL l,c; scanf("%d%d%lld%lld",&u,&v,&l,&c); G[u].push_back({u,v,l,c}); G[v].push_back({v,u,l,c}); } LL ans; priority_queue <NODE> q; while(!q.empty()) q.pop(); q.push({1,1,0,INF}); while(!q.empty()) { NODE e=q.top(); q.pop(); if(e.v==n) { ans=e.L+X/e.C; break; } int len=G[e.v].size()-1; inc(i,0,len) { NODE t=G[e.v][i]; if(t.v==e.u) continue; q.push({e.v,t.v,e.L+t.L,min(e.C,t.C)}); } } printf("%lld\n",ans); } return 0; }
USACO Milk Routing /// 優先隊列廣搜