• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 省赛傻逼题 F

哎,现场一道煞笔题又没有出!

Problem F

 Time limit: 1.000 seconds

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

Output for the Sample Input

Case 1: 20
Case 2: 9

The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Md. Mahbubul Hasan

 

 1 #include <map>
 2 #include <queue>
 3 #include <vector>
 4 #include <string>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 #define maxn 666
11 #define ll long long
12 #define INF 0x7fffffff
13 struct Edge{int from,to,a,b,t;};
14 vector<int>G[maxn];
15 vector<Edge>edges;
16 int n,m;
17 int d[maxn];
18 int in[maxn];
19 void AddEdge(int from,int to,int a,int b,int t){
20     edges.push_back((Edge){from,to,a,b,t});
21     int m=edges.size();
22     G[from].push_back(m-1);
23 }
24 void init(){
25     for(int i=0;i<n;i++)G[i].clear();
26     edges.clear();
27 }
28 int SPFA(int s){
29     queue<int>q;
30     for(int i=0;i<n;i++)d[i]=INF;
31     d[s]=0;in[s]=1;q.push(s);
32     while(!q.empty()){
33         int u=q.front();q.pop();
34         for(int i=0;i<G[u].size();i++){
35             Edge e=edges[G[u][i]];
36             int wt;
37             if(e.a-d[u]%(e.a+e.b)>=e.t)wt=0;
38             else wt=e.a+e.b-d[u]%(e.a+e.b);
39             if(e.a<e.t){
40                 if(d[u]%(e.a+e.b)<=e.a){
41                     if((e.t+d[u]%(e.a+e.b))%(e.a+e.b)<=e.a)wt=0;
42                     else wt=e.b-(e.t+d[u]%(e.a+e.b))%(e.a+e.b);
43                 }
44                 else {
45                     if(e.t%(e.a+e.b)<=e.a);
46                     else wt+=e.b-e.t%(e.a+e.b);
47                 }
48             }
49             if(d[e.to]>d[u]+e.t+wt){
50                 d[e.to]=d[u]+e.t+wt;
51                 if(!in[e.to]){
52                     in[e.to]=1;
53                     q.push(e.to);
54                 }
55             }
56         }
57         in[u]=0;
58     }
59 }
60 int main(){
61     int s,e;
62     int cas=1;
63     while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF){
64         init();
65         for(int i=0;i<m;i++){
66             int u,v,a,b,t;
67             scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
68             AddEdge(u-1,v-1,a,b,t);
69             //AddEdge(v-1,u-1,a,b,t);
70         }
71         printf("Case %d: ",cas++);
72         SPFA(s-1);
73         printf("%d\n",d[e-1]);
74     }
75     return 0;
76 }
View Code 2013-10-14 19:19:01 

 

posted @ 2013-10-14 19:21  HaibaraAi  阅读(139)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3