您的位置:首页 > 其它

文章标题 csu1333 :Funny Car Racing(最短路 spfa)

2016-09-03 23:23 579 查看

Funny Car Racing

Description

There is a funny car racing in a city with n junctions and m directed roads.

The funny part is: each road is open and closed periodically. Each road is associate with two integers (a, b), that means the road will be open for a seconds, then closed for b seconds, then open for a seconds… All these start from the beginning of the race. You must enter a road when it’s open, and leave it before it’s closed again.

Your goal is to drive from junction s and arrive at junction t as early as possible. Note that you can wait at a junction even if all its adjacent roads are closed.

Input

There will be at most 30 test cases. The first line of each case contains four integers n, m, s, t (1<=n<=300, 1<=m<=50,000, 1<=s,t<=n). Each of the next m lines contains five integers u, v, a, b, t (1<=u,v<=n, 1<=a,b,t<=105), that means there is a road starting from junction u ending with junction v. It’s open for a seconds, then closed for b seconds (and so on). The time needed to pass this road, by your car, is t. No road connects the same junction, but a pair of junctions could be connected by more than one road.

Output

For each test case, print the shortest time, in seconds. It’s always possible to arrive at t from s.

Sample Input

3 2 1 3

1 2 5 6 3

2 3 7 7 6

3 2 1 3

1 2 5 6 3

2 3 9 5 6

Sample Output

Case 1: 20

Case 2: 9

题意:给你n个点和m条边,每条边包含三个参数(开启的时间,关闭的时间和通过这条边的时间)的有向图,当你到达一个节点,如果剩余开启的时间不足够通过这条边,那么就得等下一个开放的期间,求从u到v的最短时间。

分析: 用spfa算法,当到达i节点时,我们可以确定前i-1的节点所用的时间为最优时间。

第一个例子:当从1-2时,用了3秒,接着从2-3时,开启剩余时间为(7-3%(7+7))=4<7,所以不能通过,所以得等下一个开启的时间,等待的时间为(4+7)=11;最后所用总时间为(3+11=6)=20。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int n,m,s,t;
struct edge{
int v,a,b,time;
int nex;
};
edge e[50005];
int first[50005];
int tot;
void add(int u,int v,int a,int b,int time,int &k){
e[k].v=v, e[k].a=a, e[k].b=b, e[k].time=time, e[k].nex=first[u],first[u]=k++;
}
int flag[50005];
int dis[50005];//到达i所用的最短时间
void init(){
memset (first,-1,sizeof (first));
memset (flag,0,sizeof (flag));
tot=0;
}
int spfa(int s){//起点为s
queue<int>q;
memset (dis,inf,sizeof (dis));
flag[s]=1,dis[s]=0;
q.push(s);
while (!q.empty()){
int u=q.front() ;
q.pop() ;
flag[u]=0;
for (int i=first[u];i!=-1;i=e[i].nex){
int v=e[i].v;
int time_i=e[i].a + e[i].b;//第i条边的关闭和开启的一周期用的时间
//用(dis[u]%time_i) 求出到达第i条边时到达这条边的周期的那个时间点
//而ans表示开启的时间还剩多少,如果结果为负,说明此时在关闭期间
int ans=e[i].a - (dis[u]%time_i);
if (ans<e[i].time){
ans+=e[i].b;//如果剩下的时间不够通过就得等下一个周期,所以得加上关闭的时间b;
}
else ans=0;//如果时间够用
if (dis[v]>dis[u]+e[i].time+ans){
dis[v]=dis[u]+e[i].time+ans;//如果通过这条边的时间比之前用的时间短,就代替他
if(flag[v]==0){
flag[v]=1;
q.push(v);
}
}
}
}
return dis[t];
}
int main ()
{
int cnt=1;
while (scanf ("%d%d%d%d",&n,&m,&s,&t)!=EOF){
init();
int u,v,a,b,time;
for (int i=0;i<m;i++){
scanf ("%d%d%d%d%d",&u,&v,&a,&b,&time);
if (a>=time)//判断此条路能否保证通过
add(u,v,a,b,time,tot);
}
printf ("Case %d: %d\n",cnt++,spfa(s));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 最短路