Minimum time

点击打开链接


Minimum time

TimeLimit: 1 Second   MemoryLimit: 64 Megabyte

Totalsubmit: 140   Accepted: 52  

Description

Tom gets up late today. In order to go to work faster, Tom asks you to calculate a best path for him. Tom takes the car to go to corp., and he will not stop until the traffic light is red when he arrives at the crossing. What's the minimum time Tom will take? You can suppose that the value of Tom's speed is 1,and all the traffic lights are red when Tom sets out to go to corp. The time of the traffic light being red is the same as the time of the traffic light being green, and all traffic lights have only these two colors. The places of Tom' home and corp. have no traffic lights.

Input

The first line of input contains a integer N (0<N<100) indicating the number of test cases. The first line of one test case contains a integer M (0<M<100), followed by M lines, and each line contains the data like "a b 20 10". It shows that Tom can reach b from a, and the distance between a and b is 20,and the time of the traffic light at b being red(or being green) is 10.The last line of one test case contains the names of Tom's home and corp. All names consist of one lower-case letter.

Output

Print the minimum time Tom takes in one line for one test case.

Sample Input

1
4
t a 20 10
t b 10 9
b a 10 11 
a c 20 0
t c

Sample Output

40



单源最短路径的变形,只不过路径由两部分构成而已。

注意到是红灯的时候可能红灯已经高了几分钟了,所以要减去这段时间。

还有为了减少Gegchar() 接收时处理回车与空格。可以使用一个字符串接收单个字符。

具体看代码,如有不懂,可以评阅,我会尽快回复。








#include<stdio.h>
#include<stdlib.h>
#define INF 0x7fffffff

typedef struct{
    int val;
    int time;
}NODE;
NODE g[26][26];
void Init(){
    int i,j;
    for(i=0;i<26;i++){
        for(j=0;j<26;j++){
            g[i][j].val = INF;
        }
    }
}

int dijkstra(int s,int e){
    int i,j,k,next;
    int tag[26],w[26];
    for(i=0;i<26;i++){
        tag[i] = 0;
        w[i] = INF;
    }
    tag[s] = 1;
    w[s] = 0;
    for(i=s;i!=-1;){
        tag[i] = 1;
        next = -1;
        if(i==e) return w[i];
        for(j=0;j<26;j++){
            if(!tag[j] && g[i][j].val!=INF){
                if(g[i][j].time == 0 || ((w[i]+g[i][j].val)/g[i][j].time)%2==1){
                    if(g[i][j].val + w[i] < w[j]) w[j] = w[i] +g[i][j].val;
                }
                else{
                    if(g[i][j].time - (w[i]+g[i][j].val)%g[i][j].time + w[i] + g[i][j].val < w[j]){
                        w[j] = g[i][j].time - (w[i]+g[i][j].val)%g[i][j].time + w[i] + g[i][j].val;
                    }
                }
            }
            if(!tag[j] && (next == -1 || w[j]<w[next])) next = j;
        }
       i = next;
    }
}

int main(){
    int T;
    int n,i,j,k;
    int x,y,gx,gy;
    char starts[2],ends[2];
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        Init();
        for(i=0;i<n;i++){
            scanf("%s%s%d%d",starts,ends,&x,&y);
            gx = starts[0]-'a';
            gy = ends[0]-'a';
            g[gx][gy].val = x;
            g[gx][gy].time = y;
        }
        scanf("%s%s",starts,ends);
        printf("%d\n",dijkstra(starts[0]-'a',ends[0]-'a'));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值