Minimum time

来源:互联网 发布:淘宝上如何搜同款 编辑:程序博客网 时间:2024/05/17 06:39

点击打开链接


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 0x7ffffffftypedef 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;}


0 0
原创粉丝点击