Choose the best route

本文探讨了使用SPFA算法解决城市公交路线最短路径问题的方法,详细介绍了输入输出格式、问题背景及算法实现过程,并通过实例展示了如何在有限时间内求解最优路径,对于城市交通规划具有实际应用价值。

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

 

转载于:https://www.cnblogs.com/Wurq/articles/3929293.html

FIB sources There are various entities in the system that can add routes to the FIB tables. Each of these entities is termed a source. When the same prefix is added by different sources the FIB must arbitrate between them to determine which source will contribute the forwarding information. Since each source determines the forwarding information using different best path and loop prevention algorithms, it is not correct for the forwarding information of multiple sources to be combined. Instead the FIB must choose to use the forwarding information from only one source. This choice is based on a static priority assignment 4. The FIB must maintain the information each source has added so it can be restored should that source become the best source. VPP has two control-plane sources; the API and the CLI the API has the higher priority. Each source data is represented by a fib_entry_src_t object of which a fib_entry_t maintains a sorted vector. The following configuration: set interface ip address GigabitEthernet0/8/0 192.168.1.1/24 results in the addition of two FIB entries; 192.168.1.0/24 which is connected and attached, and 192.168.1.1/32 which is connected and local (a.k.a. receive or for-us). A prefix is connected when it is applied to a router’s interface. Both prefixes are interface sourced. The interface source has a high priority, so the accidental or nefarious addition of identical prefixes does not prevent the router from correctly forwarding. Packets matching a connected prefix will generate an ARP request for the packets destination address, this process is known as a glean. An attached prefix also results in a glean, but the router does not have its own address in that sub-net. The following configuration will result in an attached route, which resolves via an attached path; ip route add table X 10.10.10.0/24 via gre0 as mentioned before, these are only appropriate for point-to-point links. If table X is not the table to which gre0 is bound, then this is the case of an attached export (see the section Attached Export). Adjacency source FIB entries Whenever an ARP entry is created it will source a fib_entry_t. In this case the route is of the form: ip route add table X 10.0.0.1/32 via 10.0.0.1 GigabitEthernet0/8/0 This is a host prefix with a path whose next-hop address is the same host. This route highlights the distinction between the route’s prefix - a description of the traffic to match - and the path - a description of where to send the matched traffic. Table X is the same table to which the interface is bound. FIB entries that are sourced by adjacencies are termed adj-fibs. The priority of the adjacency source is lower than the API source, so the following configuration: set interface address 192.168.1.1/24 GigabitEthernet0/8/0 ip arp 192.168.1.2 GigabitEthernet0/8/0 dead.dead.dead ip route add 192.168.1.2 via 10.10.10.10 GigabitEthernet1/8/0 will forward traffic for 192.168.1.2 via GigabitEthernet1/8/0. That is the route added by the control plane is favoured over the adjacency discovered by ARP. The control plane, with its associated authentication, is considered the authoritative source. To counter the nefarious addition of adj-fibs, through the nefarious injection of adjacencies, the FIB is also required to ensure that only adj-fibs whose less specific covering prefix is attached are installed in forwarding. This requires the use of cover tracking, where a route maintains a dependency relationship with the route that is its less specific cover. When this cover changes (i.e. there is a new covering route) or the forwarding information of the cover is updated, then the covered route is notified. Adj-fibs that fail this cover check are not installed in the fib_table_t’s forwarding table, they are only present in the non-forwarding table. Overlapping sub-nets are not supported, so no adj-fib has multiple paths. The control plane is expected to remove a prefix configured for an interface before the interface changes VRF.翻译一下
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值