D - Funny Car Racing
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.
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.
For each test case, print the shortest time, in seconds. It's always possible to arrive at t from s.
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
Case 1: 20
Case 2: 9题意:题意给你n个点m条边和起点位置和终点位置,然后开放的时间,关闭的时间, 每个点有一个通过的时间,只有在开放时间才能过那个点,如果要过那个点的通过的时间大于那个点的开放的时间,那么这条边,就没用。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
#define INF 999999999;
struct edge
{
int from,to;
int lu;
int red;
int w;
int id;
};
struct node
{
int x;
long long int step;
int father;
friend bool operator < (node n1,node n2)
{
return n1.step>n2.step; //<为从大到小,>大于从小到大;
}
};
vector<edge>edges;
vector<int>G[301];
bool vis[50001*2];
int n,m,start,end1;
int ans;
int tot;
void addedge(int x,int y,int lu,int red,int w)
{
edge a={x,y,lu,red,w,tot++};
edges.push_back(a);
G[x].push_back(edges.size()-1);
}
void bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node>q;
node a={start,0,0};
q.push(a);
vis[0]=1;
while(!q.empty())
{
a=q.top();
q.pop();
if(a.x==end1)
{
if(ans>a.step) ans=a.step;
continue;
}
if(a.step>ans) continue;
for(int i=0;i<G[a.x].size();i++)
{
edge b=edges[G[a.x][i]];
if(vis[b.id]) continue;
vis[b.id]=1;
int kk=a.step%(b.red+b.lu);
int xx;
if(kk>b.lu)
{ kk=kk-b.lu;
xx=a.step+(b.red-kk)+b.w;
node bb={b.to,xx,a.x};
q.push(bb);
}
else
{
if(b.w<=b.lu-kk)
{
node bb={b.to,a.step+b.w,a.x};
q.push(bb);
}
else
{
node bb={b.to,a.step+(b.lu-kk)+b.red+b.w,a.x};
q.push(bb);
}
}
}
}
}
int main()
{ int sss=1;
while(scanf("%d %d %d %d",&n,&m,&start,&end1)!=EOF)
{
for(int i=1;i<=n;i++)
{
G[i].clear();
}
edges.clear();
tot=1;
for(int i=1;i<=m;i++)
{
int x,y,lu,red,w;
scanf("%d %d %d %d %d",&x,&y,&lu,&red,&w);
if(w>lu) continue;
addedge(x,y,lu,red,w);
}
/* for(int i=0;i<edges.size();i++)
{
printf("%d ",edges[i].id);
}
printf("\n");*/
ans=999999999;
bfs();
printf("Case %d: %d\n",sss++,ans);
}
}