題目描述:
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
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
解題報告:
1:正向建圖跑一遍,再反向建圖跑一遍,最後兩個dis數組加一下取個最大值就行了。
2:模板內的數組等定義請看這篇博客:https://mp.csdn.net/console/editor/html/104326392
代碼1:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0xffffffff;
ll g[N][N], dis1[N], dis2[N];
bool visit[N];
struct point{
ll dis, id;
bool operator < (point x)const {
return dis > x.dis;
}
}p1, p2;
priority_queue<point> Q;
void init1(ll n, ll m){
for(ll i=0; i<n; ++i)
for(ll j=0; j<n; ++j)g[i][j] = INF;
for(ll i=0; i<n; ++i)g[i][i] = 0, dis1[i] = INF, visit[i] = 0, dis2[i] = INF;
while(!Q.empty())Q.pop();
for(ll i=0; i<m; ++i){
ll u, v, s;
scanf("%lld%lld%lld", &u, &v, &s);
g[--u][--v] = min(g[u][v], s);
}
}
void init2(ll n){
for(ll i=0; i<n; ++i){
for(ll j=i+1; j<n; ++j){
swap(g[i][j], g[j][i]);
}
visit[i] = 0;
}
}
void Dijkstra(ll s, ll n, ll dis[N]){
p1.id = s, p1.dis = 0, dis[s] = 0;
Q.push(p1);
while(!Q.empty()){
p1 = Q.top();
Q.pop();
if(visit[p1.id])continue;
visit[p1.id] = 1;
for(ll i=0; i<n; ++i){
if(!visit[i] && dis[p1.id]+g[p1.id][i] < dis[i]){
dis[i] = p2.dis = dis[p1.id]+g[p1.id][i];
p2.id = i, Q.push(p2);
}
}
}
}
int main(){
ll n, m, x;
while(~scanf("%lld%lld%lld", &n, &m, &x)){
init1(n, m);
Dijkstra(x-1, n, dis1);
init2(n);
Dijkstra(x-1, n, dis2);
ll ans = 0;
for(ll i=0; i<n; ++i)ans = max(ans, dis1[i]+dis2[i]);
printf("%lld\n", ans);
}
return 0;
}
代碼2:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0x3f3f3f3f;
ll g[N][N], cnt[N], dis1[N], dis2[N];
bool visit[N];
struct point{
ll id, dis;
}p1, p2;
queue<point> Q;
void init(ll n, ll m){
for(ll i=0; i<n; ++i)
for(ll j=0; j<n; ++j)g[i][j] = INF;
for(ll i=0; i<n; ++i)dis1[i] = INF, cnt[i] = 0, visit[i] = 0, g[i][i] = 0;
while(!Q.empty())Q.pop();
for(ll i=0; i<m; ++i){
ll u, v, s;
scanf("%lld%lld%lld", &u, &v, &s);
g[--u][--v] = min(g[u][v], s);
}
}
void init1(ll n){
for(ll i=0; i<n; ++i)
for(ll j=i+1; j<n; ++j)swap(g[i][j], g[j][i]);
for(ll i=0; i<n; ++i)cnt[i] = 0, dis2[i] = INF, visit[i] = 0;
while(!Q.empty())Q.pop();
}
bool SPFA(ll s, ll n, ll dis[N]){
p1.id = s, p1.dis = 0, dis[s] = 0, cnt[s] = 1;
Q.push(p1);
while(!Q.empty()){
p1 = Q.front();
Q.pop(), visit[p1.id] = 0;
for(ll i=0; i<n; ++i){
if(dis[p1.id]+g[p1.id][i] >= dis[i])continue;
p2.dis = dis[i] = dis[p1.id]+g[p1.id][i];
if(visit[i])continue;
visit[i] = 1, p2.id = i, cnt[i]++;
if(cnt[i] > n)return 0;
Q.push(p2);
}
}
return 1;
}
int main(){
ll n, m, x;
while(~scanf("%lld%lld%lld", &n, &m, &x)){
init(n, m);
SPFA(x-1, n, dis1);
init1(n);
SPFA(x-1, n, dis2);
ll maxn = 0;
for(ll i=0; i<n; ++i)maxn = max(maxn, dis1[i]+dis2[i]);
printf("%lld\n", maxn);
}
return 0;
}