POJ1716的变形,数据规模增大,用了循环数组。
//9063355 NKHelloWorld 1201 Accepted 30788K 469MS C++ 1825B 2011-08-02 19:12:01 #include <cstdio> using namespace std; int n,maxb = 0,dis[51000]; struct EDGE { int from,to,d; }; EDGE edge[51000][50]; int pedge[51000]; int spfa(int s) { int i,a,b,d; bool inque[51000]; int que[51000],tail = 0,head = 0; for(i=0;i<=maxb;i++) { inque[i] = false; dis[i] = -2147483647; } dis[s] = 0; inque[s] = true; que[tail++] = s; while(head<tail) { a = que[head%maxb]; inque[a] = false; head++; for(i=0;i<pedge[a];i++) { b = edge[a][i].to; d = edge[a][i].d; if(dis[b] < dis[a]+d) { dis[b] = dis[a]+d; if(inque[b]==false) que[tail++%maxb] = b; } } } return 0; } int main() { int i,j,a,b,d; EDGE now; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d%d%d",&a,&b,&d); now.from = a; now.to = b+1; now.d = d; if(now.to > maxb) maxb = now.to; edge[a][pedge[a]++] = now; } for(i=1;i<=maxb;i++) { now.from = i; now.to = i-1; now.d = -1; edge[now.from][pedge[now.from]++] = now; now.from = i-1; now.to = i; now.d = 0; edge[now.from][pedge[now.from]++] = now; } for(i=0;i<=maxb;i++) { now.from = maxb+1; now.to = i; now.d = 0; edge[now.from][pedge[now.from]++] = now; } spfa(maxb+1); printf("%d\n",dis[maxb]-dis[0]); return 0; }