题目
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
题解
本题中的边是单向边。我们可以通过在X点跑一遍Dij得到他们回到各个点的距离,因此本题关键点在于如何求各个点到X的距离,如果在每个点上跑一次Dij,显然会超时。若将所有的边反过来,在X点跑一遍Dij,那么此时X点到各个点的距离就是这个点到X点的最短距离。通过矩阵转置,或者开两个边数组都是可以做到的。最后统计一下就好了。
AC代码
#include <iostream> #include <algorithm> #include <queue> #include <utility> #include <cstdio> using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int , int> node; struct Edge{ int to, cost, next; }edge1[100000 + 10], edge2[100000 + 10]; //edge1是正常边,edge2是反向边 int head1[1000 + 10], head2[1000 + 10]; int cnt; void addedge(int u, int v, int w){ edge1[cnt].next = head1[u]; edge1[cnt].to = v; edge1[cnt].cost = w; head1[u] = cnt; edge2[cnt].next = head2[v]; edge2[cnt].to = u; edge2[cnt].cost = w; head2[v] = cnt++; } int dis1[1000 + 10], dis2[1000 + 10]; void dij1(int x){ //从x回到各点的距离 fill(dis1, dis1 + 1000 + 10, INF); dis1[x] = 0; priority_queue<node, vector<node>, greater<node> > q; q.push(node(0, x)); while(!q.empty()){ node temp = q.top(); q.pop(); if(temp.first > dis1[temp.second]) continue; for(int i = head1[temp.second]; i != -1; i = edge1[i].next){ if(edge1[i].cost + temp.first < dis1[edge1[i].to]){ dis1[edge1[i].to] = edge1[i].cost + temp.first; q.push(node(dis1[edge1[i].to], edge1[i].to)); } } } } void dij2(int x){ //求各点到x的距离,利用反向边可一次性得到 fill(dis2, dis2 + 1000 + 10, INF); dis2[x] = 0; priority_queue<node, vector<node>, greater<node> > q; q.push(node(0, x)); while(!q.empty()){ node temp = q.top(); q.pop(); if(temp.first > dis2[temp.second]) continue; for(int i = head2[temp.second]; i != -1; i = edge2[i].next){ if(edge2[i].cost + temp.first < dis2[edge2[i].to]){ dis2[edge2[i].to] = edge2[i].cost + temp.first; q.push(node(dis2[edge2[i].to], edge2[i].to)); } } } } int main(){ int n, m, x; //cin >> n >> m >> x; scanf("%d%d%d", &n, &m, &x); int a, b, c; cnt = 0; fill(head1, head1 + 1000 + 10, -1); fill(head2, head2 + 1000 + 10, -1); for(int i = 1; i <= m; ++i){ //cin >> a >> b >> c; scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); } dij1(x); dij2(x); int res = 0; for(int i = 1; i <= n; ++i) res = max(res, dis1[i] + dis2[i]); cout << res << endl; return 0; }