Choose the best route

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7206    Accepted Submission(s): 2352

Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
 

 

Input
There are several test cases.  Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home. Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes . Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
 

 

Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 

 

Sample Input
5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
 

 

Sample Output
1
-1
 

 

Author
dandelion
 

 

Source
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 999999
 4 int Map[1006][1006];
 5 void SPFA(int s,int m,int *Dis)        
 6 {
 7     int i,k,Start=0,End=1;
 8     int Sign[10086],Visit[10086];
 9     Sign[Start] = s;
10     Dis[s] = 0;
11     while(Start<End)
12     {
13         k=Sign[Start++];
14         Visit[k]=0;
15         for(i=1;i<=m;i++)
16         {
17             if(Map[k][i]>0&&Dis[i]>Map[k][i]+Dis[k])
18             {
19                 Dis[i]=Dis[k]+Map[k][i];
20                 if(!Visit[i])
21                 {
22                     Sign[End++]=i;
23                     Visit[i]=1;
24                 }
25             }
26         }
27     }
28 }
29 int main()
30 {
31     int i,j,T,N,a,b,c,Dis[10086],Part,MIN;
32     while(scanf("%d%d%d",&N,&T,&Part)!=EOF)
33     {
34         for(i=1;i<=N;i++)
35         {
36             Dis[i]=INF;
37             for(j=1;j<=N;j++)
38                 if(i==j)Map[i][j]=0;
39                 else Map[i][j]=INF;
40         }
41         for(i=0;i<T;i++)
42         {
43             scanf("%d%d%d",&a,&b,&c);
44             if(Map[a][b]>c) 
45                 Map[a][b]=c;
46         }
47         scanf("%d",&T);
48         for(i=1,MIN=INF+1;i<=T;i++)
49         {
50             scanf("%d",&c);
51             SPFA(c,N,Dis);
52             if(Dis[Part]!=INF&&Dis[Part]<MIN)
53             {
54                 MIN=Dis[Part];
55             }
56         }
57         if(MIN==INF+1)
58             printf("-1\n");
59         else
60             printf("%d\n",MIN);
61 
62     }
63 }
View Code

 题目大意:

  求任意一点到终点的距离,反向建图,直接用SPFA求出,终点到任意一点的距离即可。

临街矩阵:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 99999999
 4 #define MAX 1016
 5 using namespace std;
 6 #define INF 999999
 7 int Map[1006][1006];
 8 void SPFA(int s,int m,int *Dis)
 9 {
10     int i,k,Start=0,End=1;
11     int Sign[10086],Visit[10086];
12     Sign[Start] = s;
13     Dis[s] = 0;
14     while(Start<End)
15     {
16         k=Sign[Start++];
17         Visit[k]=0;
18         for(i=1;i<=m;i++)
19         {
20             if(Map[k][i]>0&&Dis[i]>Map[k][i]+Dis[k])
21             {
22                 Dis[i]=Dis[k]+Map[k][i];
23                 if(!Visit[i])
24                 {
25                     Sign[End++]=i;
26                     Visit[i]=1;
27                 }
28             }
29         }
30     }
31 }
32 int main()
33 {
34     int i,j,T,N,a,b,c,Dis[10086],Part,MIN;
35     while(scanf("%d%d%d",&N,&T,&Part)!=EOF)
36     {
37         for(i=1;i<=N;i++)
38         {
39             Dis[i]=INF;
40             for(j=1;j<=N;j++)
41                 if(i==j)Map[i][j]=0;
42                 else Map[i][j]=INF;
43         }
44         for(i=0;i<T;i++)
45         {
46             scanf("%d%d%d",&a,&b,&c);
47             if(Map[b][a]>c) /*反向建图*/
48                 Map[b][a]=c;
49         }
50         SPFA(Part,N,Dis);
51         scanf("%d",&T);
52         for(i=1,MIN=INF;i<=T;i++)
53         {
54             scanf("%d",&c);
55            // SPFA(N,N,Dis);
56             if(Dis[c]!=INF&&Dis[c]<MIN)
57             {
58                 MIN=Dis[c];
59             }
60         }
61         if(MIN==INF)
62             printf("-1\n");
63         else
64             printf("%d\n",MIN);
65 
66     }
67     return 0;
68 }
View Code

临街表:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 99999999
 4 #define MAX 10106
 5 
 6 using namespace std;
 7 int First[MAX];
 8 struct edge{int TO,Next,Vlaue;}ID[MAX*300];
 9 int SIGN;
10 
11 void Add_E(int x,int y,int z)   /*添加边*/
12 {
13     ID[SIGN].TO=y;
14     ID[SIGN].Vlaue=z;
15     ID[SIGN].Next=First[x];
16     First[x]=SIGN++;
17 }
18 void Jude(int x,int y,int c)/*判断该边是否已经存在,未存在新增边*/
19 {           /*如果已经存在,则先找到该条边,重新判断其权值的大小*/
20     int i;
21     int TMD=1;/*TMD=1,需要新加边,TMD=2,表示更新边权值,TMD=0,不需要操作*/
22     for(i=First[x];i!=0;i=ID[i].Next)   //查找与该点相关的点
23     {
24        if(ID[i].TO==y)/*如果能够找到该边*/
25        {
26             TMD=0;
27             if(ID[i].Vlaue>c)/*取权值最小的值*/
28             {
29                 ID[i].Vlaue=c;
30                 TMD=2;break;
31             }
32         }
33     }
34     if(TMD==1)
35     {
36         Add_E(x,y,c);/*无线图,所以需要左右两边*/
37       //  Add_E(y,x,c);/*无线图,所以需要左右两边*/
38     }
39     return ;
40 }
41 void SPFA(int s,int m,int *Dis)
42 {
43     int i,k,Start=0,End=1;
44     int Sign[MAX],Visit[MAX];
45     Sign[Start] = s;
46     Dis[s] = 0;
47     while(Start<End)
48     {
49         k=Sign[Start++];
50         Visit[k]=0;
51         for(i=First[k];i!=0;i=ID[i].Next)   //查找与该点相关的点
52         {
53             if(ID[i].Vlaue>0&&Dis[ID[i].TO]>ID[i].Vlaue+Dis[k])
54             {
55                 Dis[ID[i].TO]=Dis[k]+ID[i].Vlaue;
56                 if(!Visit[ID[i].TO])
57                 {
58                     Sign[End++]=ID[i].TO;
59                     Visit[ID[i].TO]=1;
60                 }
61             }
62         }
63     }
64 }
65 int main()
66 {
67     int i,j,T,N,a,b,c,Dis[10086],Part,MIN;
68     while(scanf("%d%d%d",&N,&T,&Part)!=EOF)
69     { /*求任意一起点到固定终点的距离*/
70         SIGN=1;
71         for(i=1;i<=N;i++)
72         {
73             Dis[i]=INF;
74             First[i]=0;
75         }
76          for(i=0;i<T;i++)
77         {
78             scanf("%d%d%d",&a,&b,&c);
79             Jude(b,a,c);
80         }
81         SPFA(Part,N,Dis);
82         scanf("%d",&T);
83         for(i=1,MIN=INF;i<=T;i++)
84         {
85             scanf("%d",&c);
86            // SPFA(N,N,Dis);
87             if(Dis[c]!=INF&&Dis[c]<MIN)
88             {
89                 MIN=Dis[c];
90             }
91         }
92         if(MIN==INF)printf("-1\n");
93         else printf("%d\n",MIN);
94     }
95     return 0;
96 }
View Code

 

posted @ 2014-08-22 14:03  Wurq  阅读(143)  评论(0编辑  收藏  举报