CSU 1333:funny Car Racing Shortest circuit

Source: Internet
Author: User

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]&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.