UVA 12661 Funny Car Racing(最短路)

题意:在一个赛车比赛中,有n个交叉点和m条单向道路,每条路都是周期性关闭的,求从S到T的最短路

思路:本题是明显的最短路,不过不同的是花费的总时间并不是经过每条边的通过时间之和,还要加上在每个点等待的总时间。分情况讨论一下即可


#include<bits/stdc++.h>
using namespace std;
const int maxn = 500;
const int INF = 1e9;
struct Edge
{
	int from,to,a,b,dist;
	Edge(int u,int v,int a,int b,int d):from(u),to(v),a(a),b(b),dist(d){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool inq[maxn];
int n,m,s,t,d[maxn];
void init()
{
	for (int i = 0;i<=n;i++)
		G[i].clear();
	edges.clear();
}
void AddEdge(int u,int v,int a,int b,int w)
{
	edges.push_back(Edge(u,v,a,b,w));
	int mm = edges.size();
	G[u].push_back(mm-1);
}
void spfa()
{
	memset(inq,0,sizeof(inq));
	for (int i = 0;i<n;i++)
		d[i]=INF;
	queue<int> q;
	d[s]=0;
	inq[s]=1;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		inq[u]=0;
		for (int i = 0;i<G[u].size();i++)
		{
			Edge &e = edges[G[u][i]];
			//int v = e.to;
			int a = e.a,b = e.b;
			if (a<e.dist)
				continue;
			int now = d[u]%(a+b);
			if (now + e.dist<=a)
			{
				if (d[e.to]>d[u]+e.dist)
				{	
					d[e.to] = d[u]+e.dist;
				    q.push(e.to);
				    inq[e.to]=1;
				}
			}
			else
			{
                int wait = a+b-now;
				if (d[e.to]>d[u]+wait+e.dist)
				{
					d[e.to] = d[u]+wait+e.dist;
					q.push(e.to);
					inq[e.to]=1;
				}
			}
		}
	}
}

int main()
{
     int cas = 1;
	 while (scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF)
	 {
		 init();
		 s--,t--;
		 for (int i = 0;i<m;i++)
		 {
			 int u,v,a,b,t;
			 scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
			 AddEdge(u-1,v-1,a,b,t);
		 }
		 spfa();
		 printf("Case %d: %d\n",cas++,d[t]);
	 }
}




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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值