E - Choose the best route

公交线路图中寻找朋友的最短时间,
文章讲述了在一个公交线路图中,角色QQ利用Dijkstra算法寻找她在指定站点附近丢失朋友的最短时间,如果找不到则输出-1。

题目描述

某一天,有一个叫qq的小xx,想找到她丢失已久的朋友。然而,她比较懒,只想尽快找到。现在有一张公交线路图,以及她家附近的公交站,并且已知丢失的朋友在哪。她可以在任何站换乘公交。这些公交站从1到n编号。

输入格式

每种情况均以三个整数n,m和t开头,(n <1000,m <20000, 1 =< t <= n)n代表该城市的公交车站数量,m代表之间的有向车道数量公交车站。(也许在两个公交车站之间有几种方式。)t代表在丢失的朋友附近的公交车站。然后跟随m行,每行包含三个整数p,q,d(0 <d <= 1000)。意思是从车站p到车站q有一种办法,它将花费d分钟(注意只能单向通行)。
然后,一行具有整数s(0 <s <n)的行表示Kiki可以在开始时使用的站点数。然后,s个整数代表这些站。

输出格式

输出每个数据集包含一行:qq需要花费的最少时间,如果无法找到,只需输出“ -1”即可。

样例输入

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

样例输出

1
-1
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1005,INF = 0x3f3f3f3f;
int g[N][N], dist[N];
bool st[N];
int n, m, s;

void dijkstra()
{
	memset(dist, INF, sizeof dist);
	memset(st, false, sizeof st);

	for (int i = 1; i <= n; i++) dist[i] = g[0][i];

	for (int i = 1; i <= n; i++)
	{
		int t = -1,mi = INF;
		for (int j = 1; j <= n; j++)
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;
				
		st[t] = true;

		for (int j = 1; j <= n; j++) dist[j] = min(dist[j], dist[t] + g[t][j]);

	}
}

int main()
{
	ios::sync_with_stdio(false);

	while (cin >> n >> m >> s)
	{
		memset(g, INF, sizeof g);

		for (int i = 1; i <= m; i++)
		{
			int z, x, c;
			scanf_s("%d%d%d", &z, &x, &c);
			g[z][x] = min(g[z][x],c);
		}

		int w;
		cin >> w;

		//当有多个起点时,则设0号位置为总起点,0号位置到各个给定的起点的距离均为0
		for (int i = 0; i < w; i++)
		{
			int num;
			cin >> num;
			g[0][num] = 0;
		}

		dijkstra();

		if (dist[s] != INF) cout << dist[s] << endl;
		else cout << -1 << endl;
	}
}

 

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、付费专栏及课程。

余额充值