H - 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.

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

题意:就是有一个赛车比赛,比赛的路径上就是有关卡,然后给你你通过关卡所需要的时间,但是这关卡是有限制的,它会定时开放a秒,接着关闭b秒,现在给你起点、终点、开放的时间、关闭的时间、以及通过关卡所需时间,让你求到达终点所需的最短时间。

分析:用迪杰斯特拉算法的优化队列解决此问题

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
#define maxn 300
#define maxm 50000
struct node
{
    int x,y,l,r,s;
}edge[2*maxm+5];

int sum[maxn+5];
int vis[maxn+5];
int dis[maxn+5];
int n,m,be,end;
int cmp(node x,node y)
{
    return x.x<y.x;
}

void dijkstra()
{
    for(int c=1;c<=n;c++)
    {
        int min=INT_MAX;/*整型最大值,不用定义,可直接调用。INT_MAX定义在LIMITS.H中,浮点数对应的是FLOAT.H,部分类型的定义对应的是STDDEF.H*/
        int pos=0;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&dis[i]<min)
        {
            min=dis[i];
            pos=i;
        }
      if(min==INT_MAX) break;
      vis[pos]=1;//加入集合
      for(int i=sum[pos-1]+1;i<=sum[pos];i++)
      {
          if(!vis[edge[i].y]&&edge[i].l>=edge[i].s)//少写了等号
          {
              int lt=edge[i].l-dis[pos]%(edge[i].l+edge[i].r);//路开启所剩时间
              int cost;//通过这条路需要的时间
              if(lt>=edge[i].s)//剩余时间足够通过这条路
                cost=edge[i].s;
              else
                 cost=lt+edge[i].r+edge[i].s;//时间不够等待下次开路
              if(dis[pos]+cost<dis[edge[i].y])//更新最短路
                dis[edge[i].y]=dis[pos]+cost;
          }
      }
    }
}

int main()
{
    int T=0,end;
    while(~scanf("%d %d %d %d",&n,&m,&be,&end))
    {
        int u,v,a,b,t;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d %d %d",&u,&v,&a,&b,&t);
            edge[i]=(node){u,v,a,b,t};
            //edge[i+m]=(node){u,v,a,b,t};
            sum[u]++;//是单向边还是不是单向边
            //sum[v]++;
        }
        //sort(edge+1,edge+2*m+1,cmp);
        sort(edge+1,edge+m+1,cmp);
        for(int i=2;i<=n;i++)//i=2
            sum[i]=sum[i]+sum[i-1];
        for(int i=1;i<=n;i++)
            dis[i]=123456789;
        for(int i=sum[be-1]+1;i<=sum[be];i++)
        {
            if(edge[i].l>=edge[i].s&&dis[edge[i].y]>edge[i].s)//时间够通过这条路
                dis[edge[i].y]=edge[i].s;
        }
        memset(vis,0,sizeof(vis));
        vis[be]=1;//标记起点已经走过
        dijkstra();
        T++;
        printf("Case %d: %d\n",T,dis[end]);
    }
    return 0;
}
时间: 2024-10-07 00:09:21

H - Funny Car Racing的相关文章

HDU-1052-Tian Ji -- The Horse Racing

刚开始用的动态规划+贪心 知道转移方程,但不知如何实现 下面为别人的思想: [ 设 f [i,j] 表示齐王按从强到弱的顺序出马和田忌进行了 i 场比赛之后,田忌从"头"取了 j 匹较强的马,从"尾"取了 i-j 匹较弱的马,所能够得到的最大盈利. 状态转移方程如下: f[i,j]=max{f[i-1,j]+g[n-(i-j)+1,i],f[i-1,j-1]+g[j,i]} 其中g[i,j]表示田忌的马和齐王的马分别按照由强到弱的顺序排序之后,田忌的第 i 匹马和齐

General Problem Solving Techniques [Intermediate-1]~F - Tian Ji -- The Horse Racing

Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others. Both of Tian and the king have three horses in different classes,

HDU1052Tian Ji -- The Horse Racing

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26683    Accepted Submission(s): 7875 Problem Description Here is a famous story in Chinese history. "That was about 2

hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較,二等于在推断田的最慢的是不是比king的最快的慢,三小于则与king的最快的比較: Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe

HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输,则连一条权为-200的边. 然而我们知道,二分图的最佳匹配算法的复杂度很高,无法满足N=2000的要求. 我们不妨用贪心思想来分析一下问题.因为田忌掌握有比赛的“主动权”,他总是根据齐王所出的马来分配自己的马,所以这里不妨认为齐王的出马顺序是按马的速度从高到低出的.由这样的假设,我们归纳出如下贪心

hdu1052 Tian Ji -- The Horse Racing

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17346    Accepted Submiss

ACM: Racing Gems - 最长递增序列

Racing Gems   You are playing a racing game.  Your character starts at the x axis (y = 0) and proceeds up the      race track, which has a boundary at the line x = 0 and another at x = w.  You  may start the race       at any horizontal position you

CSU 1333: Funny Car Racing(SPFA)13年省赛题

1333: Funny Car Racing Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 482  Solved: 116 [Submit][Status][Web Board] 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 clos

Tian Ji -- The Horse Racing(杭电1052)(贪心)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18925    Accepted Submission(s): 5530 Problem Description Here is a famous story in Chinese history. "That was about