const int MAXV = 110; struct Edge { int from, to, l, r, dist; }; struct HeapNode { int d, u; bool operator < (const HeapNode& rhs) const { return d > rhs.d; } }; struct Dijkstra { int n, m; //n:点数 m:边 vector<Edge> edges; //存储所有的边 vector<int> G[MAXV];//每个点的所有相邻边序号 bool done[MAXV]; // 是否已永久标号 int d[MAXV]; // s起点到各个点的距离 int p[MAXV]; // 最短路树中的上一条边序号 void init(int n) { this->n = n; for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int l, int r, int dist) { edges.push_back((Edge) { from, to, l, r, dist }); m = edges.size(); G[from].push_back(m - 1); } void dijkstra(int s, int v) { priority_queue<HeapNode> Q; for(int i = 0; i < n; i++) d[i] = INF; d[s] = v; memset(done, 0, sizeof(done)); Q.push((HeapNode) { 0, s }); while(!Q.empty()) { HeapNode x = Q.top(); Q.pop(); int u = x.u; if(done[u]) continue; done[u] = true; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; int st = max(d[u], e.l); if(d[e.to] > st + e.dist && st + e.dist <= e.r) { d[e.to] = st + e.dist; p[e.to] = G[u][i]; Q.push((HeapNode) { d[e.to], e.to }); } } } } } dij; int main() { int n, m, s, e; while (~RIV(n, m, s, e)) { dij.init(n + 1); REP(i, m) { int u, v, l, r, c; RV(u, v, l, r, c); if (r - l < c) continue; dij.AddEdge(u, v, l, r, c); } int ans = INF; REP(i, 11000) { dij.dijkstra(s, i); if (dij.d[e] != INF) ans = min(ans, dij.d[e] - i); } if (ans != INF) WI(ans); else puts("Impossible"); } return 0; }
Escort of Dr. Who How,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wty__/article/details/38443089