Topic Connection: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333
Test instructions: give you an n vertex, M edge (each edge has three parameters, opening time, closing time, and the time of passing this edge) of the direction graph;
You need to find the shortest path from S to T; Dijkstra algorithm can be solved;
Pit point: I use the queue optimization +vector store each edge, after each call Dijkstra, must initialize the adjacency table, in this place for a long time, this is the second, after remembering if the vector to initialize ~
Attached code:
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include < vector> #include <queue>const int inf=999999999;using namespace std;typedef pair<int, int > P;int n,m,s,t; struct edge{int to; int open; int close; int cost;}; Vector<edge>g[330];int d[330];void Init () {for (int i=0;i<330;i++) g[i].clear ();} void Dijkstra () {priority_queue<p,vector<p>,greater<p> >que; for (int i=0;i<320;i++) D[i]=inf; d[s]=0; Que.push (P (0,s)); while (!que.empty ()) {P p=que.top (); Que.pop (); int V=p.second; if (D[v]<p.first) continue; for (int i=0;i<g[v].size (); i++) {Edge e=g[v][i]; int tmp=p.first,mod=e.open+e.close; int re=tmp%mod; if (re<e.open&&e.open-re>=e.cost) {if (d[e.to]>tmp+e.cost) {d[e.to]=tmp+e.c Ost Que.push (P (d[e.to],e.to)); }} else if (e.open>=e.cost) {if (D[E.TO]>tmp+e.cost+mod-re) {d[e.to]=tmp+e.cost+mod-re; Que.push (P (d[e.to],e.to)); }}}}}int main () {int test=1; while (scanf ("%d%d%d%d", &n,&m,&s,&t)!=eof) {for (int i=0;i<m;i++) {int u,v,a,b,t1; scanf ("%d%d%d%d%d", &u,&v,&a,&b,&t1); Edge e; E.to=v; E.open=a; E.close=b; E.COST=T1; G[u].push_back (e); } Dijkstra (); Init (); printf ("Case%d:%d\n", test++,d[t]); } return 0;}
CSU 1333:funny Car Racing Shortest circuit