UVA - 12661(加条件的最短路)

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 twointegers (a, b), that means the road will be open for a seconds, then closed for b seconds, then open fora seconds. . . All these start from the beginning of the race. You must enter a road when it’s open, andleave 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 youcan 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 withjunction v. It’s open for a seconds, then closed for b seconds (and so on). The time needed to pass thisroad, by your car, is t. No road connects the same junction, but a pair of junctions could be connectedby 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,s,t;n个点,m个边,求点s到点t花费的最短时间。下面给出m个边的信息:路open的时间,close的时间,走过这条路所需要的时间。(当所需时间大于open时间,这条路就没意义)。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int inf=1<<29;
const int maxn=100100;
int e,n,m,s,t,head[maxn],nxt[maxn],cost[maxn],a[maxn],b[maxn],pnt[maxn],dis[maxn];
bool vis[maxn];
queue<int >q;
void addedge(int u,int v,int c,int sa,int sd)
{
    pnt[e]=v;
    nxt[e]=head[u];
    cost[e]=c;
    a[e]=sa;
    b[e]=sd;
    head[u]=e++;
}

int spfa(int st,int des)
{
    for(int i=0;i<=n;i++)
    dis[i]=inf;
    dis[st]=0;
    memset(vis,0,sizeof(vis));
    q.push(st);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=1;
        for(int i=head[u];i!=-1;i=nxt[i])
        {
            int v=pnt[i];
            int val=dis[u],s=dis[u];
            val=val%(a[i]+b[i]);
            if(val>a[i]) s+=b[i]-(val-a[i]);
            else if(a[i]-val<cost[i]) s+=a[i]-val+b[i];
            if(s+cost[i]<dis[v])
            {
                dis[v]=s+cost[i];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[des];
}
int main()
{
  int cas=1;
  while(~scanf("%d%d%d%d",&n,&m,&s,&t))
  {
      memset(head,-1,sizeof(head));
      e=0;
      for(int i=0;i<m;i++)
      {
          int from,to,open,close,time;
          scanf("%d%d%d%d%d",&from,&to,&open,&close,&time);
          if(open>=time)addedge(from,to,time,open,close);
      }
      printf("Case %d: %d\n",cas++,spfa(s,t));
  }
}

敢想!!!!提交一直不过是因为#include<bits/stdc++.h>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值