zoukankan      html  css  js  c++  java
  • poj 3268 最短路dijkstra *

    题目大意:给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出

    链接:点我

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 using namespace std;
     9 #define MOD 1000000007
    10 #define pb(a) push_back(a)
    11 const int INF=0x3f3f3f3f;
    12 const double eps=1e-5;
    13 typedef long long ll;
    14 #define cl(a) memset(a,0,sizeof(a))
    15 #define ts printf("*****
    ");
    16 int n,m,tt,cnt;
    17 const int MAXN=1010;
    18 #define typec int
    19 bool vis[MAXN];
    20 int pre[MAXN];
    21 int cost[MAXN][MAXN];
    22 int dist1[MAXN],dist2[MAXN];
    23 void Dijkstra(typec lowcost[],int n,int beg)
    24 {
    25     for(int i=1;i<=n;i++)
    26     {
    27         lowcost[i]=INF;vis[i]=false;pre[i]=-1;
    28     }
    29     lowcost[beg]=0;
    30     for(int j=0;j<n;j++)
    31     {
    32         int k=-1;
    33         int Min=INF;
    34         for(int i=1;i<=n;i++)
    35         if(!vis[i]&&lowcost[i]<Min)
    36         {
    37             Min=lowcost[i];
    38             k=i;
    39         }
    40         if(k==-1)break;
    41         vis[k]=true;
    42         for(int i=1;i<=n;i++)
    43         if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
    44         {
    45             lowcost[i]=lowcost[k]+cost[k][i];
    46             pre[i]=k;
    47         }
    48     }
    49 }
    50 int main()
    51 {
    52     int i,j,k;
    53     #ifndef ONLINE_JUDGE
    54     freopen("1.in","r",stdin);
    55     #endif
    56     int x;
    57     while(scanf("%d%d%d",&n,&m,&x)!=EOF)
    58     {
    59         for(i=1;i<=n;i++)   for(j=1;j<=n;j++)   if(i==j)    cost[i][j]=0;else   cost[i][j]=INF;
    60         int u,v,w;
    61         for(i=0;i<m;i++)
    62         {
    63             scanf("%d%d%d",&u,&v,&w);
    64             cost[u][v]=min(cost[u][v],w);
    65         }
    66         Dijkstra(dist1,n,x);
    67         for(i=1;i<=n;i++)
    68         {
    69             for(j=1;j<i;j++)   swap(cost[i][j],cost[j][i]);
    70         }
    71         Dijkstra(dist2,n,x);
    72         int ans=0;
    73         for(i=1;i<=n;i++)
    74             ans=max(ans,dist1[i]+dist2[i]);
    75         printf("%d
    ",ans);
    76     }
    77 }
  • 相关阅读:
    安装python包
    在RHEL5.4上升级Python
    IronPython开发Windows Form程序总结
    Windows下手动配置Oracle Client的要点
    dreampie一个很不错的python命令行交互工具
    Eclipse插件汇总
    pyDbRowFactory Python版Db Row Factory
    如何让Jython自动加载一个Jar包
    跨计算机执行的几个方法
    Python 版 Instance Activator
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4587961.html
Copyright © 2011-2022 走看看