码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-25 21:40:23      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:算法   spfa   

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 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 31 2 5 6 32 3 7 7 63 2 1 31 2 5 6 32 3 9 5 6

Sample Output

Case 1: 20Case 2: 9

HINT

Source

湖南省第九届大学生计算机程序设计竞赛

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
const ll INF  = 1LL<<60 ;
const ll N = 305 ;

struct EDG
{
    ll to , next ;
    ll a , b , c , d ;
} edg[50010];
ll eid , head[N] ;
ll dis[N] ;
void init()
{
    eid = 0;
    memset(head , -1 , sizeof(head));
    for(ll i = 0 ; i < N ; i++)
        dis[i] = INF ;
}
void addEdg(ll u , ll v , ll a , ll b , ll c)
{
    edg[eid].to = v ;
    edg[eid].next = head[u] ;
    edg[eid].a = a ;
    edg[eid].b = b ;
    edg[eid].c = c ;
    edg[eid].d = a + b ;
    head[u] = eid++;
}
void spfa(ll s , ll t)
{
    queue<ll>q;
    bool inq[N] = { 0 } ;
    ll u , v ;
    dis[s] = 0 ;
    if(s != t )
        q.push( s ) ;
    while(!q.empty())
    {
        u = q.front() ;
        q.pop() ;
        inq[u] = 0 ;
        for(ll i = head[u] ; i!=-1; i=edg[i].next)
        {
            v = edg[i].to ;
            ll tt = edg[i].a - (dis[u]%edg[i].d);
            if(tt<edg[i].c)
                tt += edg[i].b ;
            else
                tt = 0 ;
            if(dis[v] > dis[u] + tt +edg[i].c)
            {
                dis[v] = dis[u] + tt +edg[i].c ;
                if(inq[v]==0&&v!=t)
                    inq[v] = 1 , q.push(v);
            }
        }
    }
}
int main()
{
    ll n , m , s , t , u , v , a , b , c ;
    ll T = 0;
    while(scanf("%lld%lld%lld%lld",&n,&m,&s,&t)>0)
    {
        init();
        while(m--)
        {
            scanf("%lld%lld%lld%lld%lld",&u , &v , &a , &b , &c) ;
            if(c<=a)
                addEdg( u , v , a , b , c );
        }
        spfa(s  , t ) ;
        if(dis[t]==INF)
            dis[t] = -1;
        printf("Case %lld: %lld\n",++T,dis[t]);
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:算法   spfa   

原文地址:http://blog.csdn.net/u010372095/article/details/47983461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!