Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18961 | Accepted: 8655 |
Description
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
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
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
Source
思路:
题意可知是最短路,单向图,求“从源点到各个点的最短路+从各个点到源点的最短路”的和的最大值,也就是“minimax”这个概念;
主角还是我们的奶牛,某个领地(注意输入会给出)出了头牛登基,所有的牛都要去朝拜他,但是路途遥远他们不愿意花太多时间在路上,所以求出到头牛领地来回一趟的最短路,然后最短路中求出最大值,因为我们要求的是最少多长时间可以所有牛都来回一趟,自然我们要求的就是“minimax”了,要不然走不完不是;
首先,我一看觉得是多源最短路问题,所以直接floyd,自然就TLE了,当然主要是我眼拙,没看到输入会给出头牛领地;
后来一看,这个多源最短路问题可以转换成单源的,从头牛领地回到各自领地就是裸的最短路,头牛领地就是源点,单源吧,问题解决了一半;
然后我们再看从各自领地到头牛领地的最短路,头牛领地就是终点,我们可以把图反着存,终点就变成源点了,单源了吧,问题解决了;
可以采用spfa或者dijkstra,跟裸算法只有一点不一样,图反着存,spfa呢就把邻接表反着存一下,dijkstra呢转置矩阵(Map[i][j]和Map[j][i]交换一下),ok!
代码1(spfa):
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <queue>
#define N 1010
#define M 100100
#define inf 1<<29
using namespace std;
//出边和入边交换,正反两次spfa
struct node{
int v;
int c;
int next;//记录编号为i的边的前一条边的编号
}edge[M];
int dis[N];
int total_dis[N];
int path[N];//记录每个顶点其中一条边的编号
int vis[N];//是否在队列中
int n, m, temp;
void add(int m, int a[], int b[], int c[])
{
memset(path, -1, sizeof(path));
memset(vis, false, sizeof(vis));
fill(dis, dis+N, inf);
//one-way
for(int i = 0; i < m; i++)
{
edge[i].v = b[i];
edge[i].c = c[i];
edge[i].next = path[a[i]];
path[a[i]] = i;
}
}
void spfa(int s)
{
dis[s] = 0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int t = Q.front();
Q.pop();
vis[t] = false;
for(int i = path[t]; i != -1; i = edge[i].next)
{
int w = edge[i].c;
int temp = edge[i].v;
if(dis[temp] > dis[t] + w)
{
dis[temp] = dis[t] + w;
if(!vis[temp])
{
Q.push(temp);
vis[temp] = true;
}
}
}
}
for(int i = 1; i <= n; i++)
total_dis[i] += dis[i];
}
int main()
{
int a[M], b[M], c[M];
fill(total_dis, total_dis+N, 0);
scanf("%d%d%d", &n, &m, &temp);
for(int i = 0; i < m; i++)
scanf("%d%d%d", &a[i], &b[i], &c[i]);
add(m, a, b, c);
spfa(temp);
add(m, b, a, c);
spfa(temp);
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans, total_dis[i]);
printf("%d", ans);
return 0;
}
代码2(dijkstra):
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <cmath>
#include <string>
#define N 1100
#define inf 1<<29
using namespace std;
//单源最短路
bool vis[N];
int Map[N][N];
int dis[N];
int total_dis[N];
int n, m, temp;
void inti()
{
int i, j;
int a, b, c;
memset(vis, false, sizeof(vis));
fill(dis, dis+N, inf);
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
Map[i][j] = inf;
Map[i][i] = 0;
}
while(m-->0)
{
scanf("%d%d%d", &a, &b, &c);
if(Map[a][b] > c)//两点之间不止一条路径
Map[a][b] = c;//有向图
}
///记录源点s到其余点的初始路径
for(i = 1; i <= n; i++)
dis[i] = Map[temp][i];
vis[temp] = true;
}
//转置矩阵
void turn()
{
int t;
memset(vis, false, sizeof(vis));
fill(dis, dis+N, inf);
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++)
{
t = Map[i][j];
Map[i][j] = Map[j][i];
Map[j][i] = t;
}
///记录源点s到其余点的初始路径
for(int i = 1; i <= n; i++)
dis[i] = Map[temp][i];
vis[temp] = true;
}
void dijkstra()
{
int i, j, k;
int min;
int t = temp;//注意赋初值
for(i = 1; i < n; i++)
{
//找出距离i点的最短路径点
min = inf;
for(j = 1; j <= n; j++)
if(!vis[j] && dis[j] < min)
{
min = dis[j];
t = j;
}
vis[t] = true;
for(k = 1; k <= n; k++)
if(dis[k] > dis[t] + Map[t][k])
dis[k] = dis[t] + Map[t][k];
}
for(int i = 1; i <= n; i++)
total_dis[i] += dis[i];
}
int main()
{
scanf("%d%d%d", &n, &m, &temp);
inti();
dijkstra();
turn();
dijkstra();
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans, total_dis[i]);
printf("%d", ans);
return 0;
}