zoukankan      html  css  js  c++  java
  • POJ 3268 Silver Cow Party(最短路)

    题面

    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-N节点的牛要到X节点开Party,问来回所需时间最大的牛所花费的时间(所有的边都是单向边)

    存正边和反边,跑2遍SPFA求最大值即可

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define MAX 1100
    #define MAXL 100100
    inline int read()
    {
    	  register int x=0,t=1;
    	  register char ch=getchar();
    	  while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    	  if(ch=='-'){t=-1;ch=getchar();}
    	  while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    	  return x*t;
    }
    struct Line
    {
    	  int v,next,w;
    }e[MAXL],E[MAXL];//正边和反向边
    int N,M,X;
    int h[MAX],H[MAX],cnt=1;
    bool vis1[MAX],vis2[MAX];
    int dis1[MAX],dis2[MAX];
    queue<int> Q; 
    inline void Add(int u,int v,int w)
    {
    	  e[cnt]=(Line){v,h[u],w};
    	  E[cnt]=(Line){u,H[v],w};
    	  h[u]=H[v]=cnt++;
    }
    int main()
    {
     	  N=read();M=read();X=read();
     	  for(int i=1;i<=M;++i)
     	  {
     	  	 int u=read(),v=read(),w=read();
     	     Add(u,v,w);
     	  } 
     	  /****SPFA1****/
     	  vis1[X]=true;
     	  for(int i=1;i<=N;++i)
     	     dis1[i]=1000000000;
     	  dis1[X]=0;
     	  Q.push(X);
     	  while(!Q.empty())
     	  {
     	  	    int u=Q.front();Q.pop();
     	  	    vis1[u]=false;
     	  	    for(int i=h[u];i;i=e[i].next)
     	  	    {
     	  	    	   int v=e[i].v,w=e[i].w;
     	  	    	   if(dis1[v]>dis1[u]+w)
     	  	    	   {
     	  	    	   	     dis1[v]=dis1[u]+w;
     	  	    	   	     if(!vis1[v])
     	  	    	   	     {
     	  	    	   	     	    vis1[v]=true;
     	  	    	   	     	    Q.push(v);
     	  	    	   	     }
     	  	    	   }
     	  	    }
     	  }
     	  /****SPFA2****/
     	  vis2[X]=true;
     	  for(int i=1;i<=N;++i)
     	     dis2[i]=1000000000;
     	  dis2[X]=0;
     	  Q.push(X);
     	  while(!Q.empty())
     	  {
     	  	    int u=Q.front();Q.pop();
     	  	    vis2[u]=false;
     	  	    for(int i=H[u];i;i=E[i].next)
     	  	    {
     	  	    	   int v=E[i].v,w=E[i].w;
     	  	    	   if(dis2[v]>dis2[u]+w)
     	  	    	   {
     	  	    	   	     dis2[v]=dis2[u]+w;
     	  	    	   	     if(!vis2[v])
     	  	    	   	     {
     	  	    	   	     	    vis2[v]=true;
     	  	    	   	     	    Q.push(v);
     	  	    	   	     }
     	  	    	   }
     	  	    }
     	  }
     	  int Ans=0;
     	  for(int i=1;i<=N;++i)
     	     Ans=max(Ans,dis1[i]+dis2[i]);
     	  cout<<Ans<<endl;
     	  return 0;
    }
    
    
    
  • 相关阅读:
    Font Awesome 中文网
    mobileselect学习
    JavaScript模块化
    webpack基本使用
    MVVM架构方式
    http-server开启测试服务器
    json-server模拟服务器API
    vue-router
    git的使用
    Vue生命周期
  • 原文地址:https://www.cnblogs.com/cjyyb/p/7242632.html
Copyright © 2011-2022 走看看