【最短路·双Dijkstra】Silver Cow Party

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30741 Accepted: 13936

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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个牛分别住在n个农场,现在要在x农场办party,每个农场的牛都要去参加,所有的牛在去和回来的时候都会选择花费时间最短的路线,现在问,在所有的牛中花费时间最长的是多少时间(注意路径是单向的所以去和回来的路可能不同)
思路:跑两遍最短路,第一遍从X到各个点,第二遍从各个点到X,把时间加起来,最多的那个就是答案。第二次跑的时候可以把矩阵转置或者一开始就反向存图。
代码:
1.邻接矩阵:(注意矩阵转置的做法)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1010
int map[maxn][maxn],n;
int way[maxn],dis[maxn];

void dijkstra(int x)
{
  int visit[maxn],i,j,min,next=x;
  memset(visit,0,sizeof(visit));
  for(i=1;i<=n;i++)
    dis[i]=map[x][i];
  visit[x]=1;
  for(i=2;i<=n;i++)
  {
    min=INF;
    for(j=1;j<=n;++j)
    {
      if(!visit[j]&&dis[j]<min)
      {
        min=dis[j];
        next=j;
      }
    }
    visit[next]=1;
    for(j=1;j<=n;++j)
    {
      if(!visit[j]&&dis[j]>dis[next]+map[next][j])
        dis[j]=dis[next]+map[next][j];
    }
  }
}

int main()
{
  int m,x,i,j,a,b,t;
  while(cin>>n>>m>>x)
  {
    for(i=1;i<=n;i++)
    {
      for(j=1;j<=n;++j)
      {
        if(i!=j)
          map[i][j]=INF;
        else
          map[i][j]=0;
      }
    }
    while(m--)
    {
      scanf("%d%d%d",&a,&b,&t);
        map[a][b]=t;
    }
    dijkstra(x);
    for(i=1;i<=n;i++)//存下来之前的结果
      way[i]=dis[i];
    int ans=0;
    for(i=1;i<=n;i++)
    {
      for(j=i+1;j<=n;++j)
        swap(map[i][j],map[j][i]);
    }
    dijkstra(x);
    for(i=1;i<=n;i++)
    {
      if(i!=x)
        ans=max(ans,way[i]+dis[i]);
    } 
    printf("%d\n",ans);
  }
  return 0;
}

2.堆优化的Dijkstra:(注意别忘了全面初始化)

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=100000;
struct qnode
{
  int v;
  int c;
  qnode(int v=0,int c=0):v(v),c(c){}
  bool operator <(const qnode &r)const
  {
    return c>r.c;
  }
};
struct store
{
  int u,v,w;
};
vector<store> re;//反向存图

struct Edge
{
  int v,cost;
  Edge(int v=0,int cost=0):v(v),cost(cost){}
};
vector<Edge>E[MAXN];//图


bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//点的编号从1开始
{
  memset(vis,false,sizeof(vis));
  for(int i=1;i<=n;i++)dist[i]=INF;
  priority_queue<qnode>que;
  while(!que.empty())que.pop();//初始化
  dist[start]=0;
  que.push(qnode(start,0));
  qnode tmp;
  while(!que.empty())
  {
    tmp=que.top();
    que.pop();
    int u=tmp.v;
    if(vis[u])continue;
    vis[u]=true;
    for(int i=0;i<E[u].size();i++)
    {
      int v=E[tmp.v][i].v;
      int cost=E[u][i].cost;
      if(!vis[v]&&dist[v]>dist[u]+cost)
      {
        dist[v]=dist[u]+cost;
        que.push(qnode(v,dist[v]));
      }
    }
  }
}
void addedge(int u,int v,int w)
{
  E[u].push_back(Edge(v,w));
}
int main()
{
  int n,m,x;
  int way_cost[100000];
  while(cin>>n>>m>>x)
  {
    for(int i=1;i<=n;i++) E[i].clear();
    for(int i=0;i<m;i++)
    {
      int u,v,w;
      scanf("%d%d%d",&u,&v,&w);
      addedge(u,v,w);
      store temp;
      temp.u=v;
      temp.v=u;
      temp.w=w;
      re.push_back(temp);
      //addedge(v,u,w);无向图
    }
    Dijkstra(n,x);
    for(int i=1;i<=m;i++)
      way_cost[i]=dist[i];
    E->clear();
    for(int i=1;i<=n;i++) E[i].clear();

    for(int i=0;i<m;i++)
    {
      addedge(re[i].u,re[i].v,re[i].w);
    }
    Dijkstra(n,x);
    int ans=-1;
    for(int i=1;i<=n;i++)
    {
      if(i!=x)
        if(ans<way_cost[i]+dist[i])
          ans=way_cost[i]+dist[i];
    } 
    printf("%d\n",ans);
  }
  return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

3 × 3 =