poj 3268 Silver Cow Party (dijkstra+置换矩阵处理)

来源:互联网 发布:jquery 1.8.3.min.js 编辑:程序博客网 时间:2024/06/08 01:00

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

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.

大致题意:有n个农场,每个农场都有一头牛,分别从1~n号农场赶往x号农场参加聚会,农场与农场之间的路是单向的,在n个农场之间有m条路,给出 a ,b , t表示从a号农场到b号农场需要t时间。 每头牛都会选择时间最短的路过去和回来,问所有的牛所花费的时间最长的是多少?

思路:我们可以将其分成两部分来计算,首先计算源点X农场到其他各农场所花费的最少时间(即牛回去所花时间)我们可以用dijkstra或者其他求最短路算法很容易的算出,接下来我们需要计算每个农场的牛到X农场的时间,但是如果直接枚举每个农场的牛到X农场的最短时间,最大时间复杂度为O(n^3),显然是会超时。考虑到道路是单向的,所以我们置换矩阵,交换 maps[i][j] 与 maps[j][i] 的值,然后就枚举过程就变成了求源点X到其他农场的最短时间,与之前思路一样,用dijkstra或者其他求最短路算法很容易的算出,然后将两次求得的值相加,取最大值输出即可。

代码如下

#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;const int INF=0x3f3f3f3f;const int maxn=1005;int maps[maxn][maxn];//存两点间的距离int dis[maxn];//表示源点到该点的最少时间 int way[maxn];//保存第一次求得的最少时间int n;//节点数 struct node{    int num;    int dis;    friend bool operator <(node a,node b)    {        if(a.dis==b.dis) return a.num>b.num;        return a.dis>b.dis;    }};priority_queue<node> que;//用优先队列优化void init(){    for(int i=1;i<=n;i++)    for(int j=1;j<=n;j++)    {        if(i==j) maps[i][j]=0;        else         maps[i][j]=INF;    }}void Dijkstra(int s){    int vis[maxn];//表示该点是否已经访问过     for(int i=1;i<=n;i++)    dis[i]=INF;    memset(vis,0,sizeof(vis));    dis[s]=0;    node u;    u.dis=0;    u.num=s;    que.push(u);    while(!que.empty())    {        node u=que.top();        que.pop();        int x=u.num;        if(vis[x])            continue;        vis[x]=1;           for(int i=1;i<=n;i++)        {            if(!vis[i]&&dis[i]>dis[u.num]+maps[u.num][i])            {                dis[i]=dis[u.num]+maps[u.num][i];                node v;                v.num=i;                v.dis=dis[i];                que.push(v);            }        }    }    //printf("%d\n",dis[n]); }  int main() {    int m,X,x,y,D;    while(scanf("%d%d%d",&n,&m,&X)!=EOF)    {        init();        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",&x,&y,&D);            maps[x][y]=D;        }        Dijkstra(X);        for(int i=1;i<=n;i++)        way[i]=dis[i];        for(int i=1;i<=n;i++)//置换矩阵        for(int j=i+1;j<=n;j++)        {            int f;            f=maps[j][i];            maps[j][i]=maps[i][j];            maps[i][j]=f;        }        Dijkstra(X);        int sum=0;        for(int i=1;i<=n;i++)        {            sum=max(sum,way[i]+dis[i]);        }        printf("%d\n",sum);     }     return 0; }
0 0