UVA-12661 Funny Car Racing (dijkstra)
题目大意:一张有向图,问从起点到终点的最快时间。不过边有点特殊,从u到v的边没开放a秒就关闭b秒。
题目分析:dijkstra算法即可。在从u走到v的时候要注意一下时间。
代码如下:
# include<iostream> # include<cstdio> # include<queue> # include<cstring> # include<algorithm> using namespace std; const int INF=1<<30; struct Edge { int u,v,a,b,t,nxt; }; Edge e[100005]; int n,m,s,ee,head[305],cnt,d[305]; void add(int u,int v,int a,int b,int t) { e[cnt].v=v,e[cnt].a=a,e[cnt].b=b,e[cnt].t=t; e[cnt].nxt=head[u],head[u]=cnt++; } void dijkstra() { fill(d,d+n+1,INF); queue<int>q; d[s]=0; q.push(s); while(!q.empty()) { int fr=q.front(); q.pop(); for(int i=head[fr];i!=-1;i=e[i].nxt){ int to=e[i].v; int time=(d[fr]%(e[i].a+e[i].b))+e[i].t; if(time>e[i].a) time=(e[i].a+e[i].b-d[fr]%(e[i].a+e[i].b))+e[i].t; else time=e[i].t; if(d[fr]+time<d[to]){ d[to]=d[fr]+time; q.push(to); } } } } int main() { int u,v,a,b,t,cas=0; while(scanf("%d%d%d%d",&n,&m,&s,&ee)==4) { cnt=0; memset(head,-1,sizeof(head)); while(m--) { scanf("%d%d%d%d%d",&u,&v,&a,&b,&t); if(t>a) continue; add(u,v,a,b,t); } dijkstra(); printf("Case %d: %d\n",++cas,d[ee]); } return 0; }