思路:d(i)表示到达节点i的最大能运输的重量,转移方程d(i) = min(d(u), limit(u, i));注意优先队列应该以重量降序排序来重载小于符号。
AC代码
#include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 1000 + 5; int d[maxn]; bool vis[maxn]; struct Edge{ int from, to, dist; Edge(){} Edge(int u, int v, int d):from(u), to(v), dist(d) {} }; vector<Edge>edge; struct HeapNode{ int d, u; HeapNode() {} HeapNode(int d, int u):d(d), u(u) {} bool operator < (const HeapNode &p) const { return d < p.d; } }; vector<int>G[maxn]; void init(int n) { edge.clear(); for(int i = 0; i <= n; ++i) G[i].clear(); } void addEdge(int from, int to, int dist) { edge.push_back(Edge(from, to, dist)); int m = edge.size(); G[from].push_back(m-1); } void dijkstra(int s) { priority_queue<HeapNode>q; memset(d, 0, sizeof(d)); d[s] = inf; memset(vis, 0, sizeof(vis)); q.push(HeapNode(inf, 1)); while(!q.empty()) { HeapNode p = q.top(); q.pop(); int u = p.u; if(vis[u]) continue; for(int i = 0; i < G[u].size(); ++i) { Edge &e = edge[G[u][i]]; int w = min(d[u], e.dist); if(d[e.to] < w) { d[e.to] = w; q.push(HeapNode(d[e.to], e.to)); } } } } int main() { int T, n, m; scanf("%d", &T); int kase = 1; while(T--) { scanf("%d%d", &n, &m); init(n); int u, v, dis; for(int i = 0; i < m; ++i) { scanf("%d%d%d", &u, &v, &dis); addEdge(u, v, dis); addEdge(v, u, dis); } dijkstra(1); printf("Scenario #%d:\n", kase++); printf("%d\n\n", d[n]); } return 0; }
如有不当之处欢迎指出!