Funny Car Racing bfs题

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 (ab), 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条边和起点位置和终点位置,然后开放的时间,关闭的时间, 每个点有一个通过的时间,只有在开放时间才能过那个点,如果要过那个点的通过的时间大于那个点的开放的时间,那么这条边,就没用。
我用的是bfs搜索过的,我这里采用给每条边,编号,判重;
代码:

#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);
  }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值