zoukankan      html  css  js  c++  java
  • HDU2066一个人的旅行(dijkstra)

    一开始拿到这个题感觉floyd可能会超,还是写了写,果然1WA+1TLE,之后觉得用dijkstra试试看看S和D会不会比较小,还是1WA+1TLE,最后还是借鉴了别人的做法。

    把他的家作为起点,与他相邻的城市作为权值为0的边,这样就转换成单源起点的最短路问题用dijkstra直接O(n^2)过

    键代码

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<map>
     5 #include<vector>
     6 #include<set>
     7 #include<stack>
     8 #include<queue>
     9 #include<algorithm>
    10 #include<stdlib.h>
    11 using namespace std;
    12 #define MAX(a,b) (a > b ? a : b)
    13 #define MIN(a,b) (a < b ? a : b)
    14 #define MAXN  10000001
    15 #define INF 1000000007
    16 #define mem(a) memset(a,0,sizeof(a))
    17 
    18 int w[1001][1001],s,t[1001],d[1001],vis[1001];
    19 int T,S,D;
    20 int key;
    21 
    22 void dijkstra(int st)
    23 {
    24     mem(vis);
    25     for(int i=1;i<=key;i++)d[i]=INF;
    26     for(int i=0;i<=key;i++)
    27     {
    28         int m=INF;
    29         for(int j=0; j<=key;j++)
    30         {
    31             if(!vis[j] && m>=d[j]) m = d[st=j];
    32         }
    33         vis[st]=1;
    34         for(int j=0;j<=key;j++)
    35         {
    36             d[j] = MIN(d[j], d[st]+w[st][j]);
    37         }
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     while(~scanf("%d%d%d",&T,&S,&D))
    44     {
    45         mem(w);
    46         int a,b,x;
    47         key = 0;
    48         for(int i=0;i<=1000;i++)
    49         {
    50             for(int j=0;j<=1000;j++)
    51             {
    52                 w[i][j] = INF;
    53             }
    54         }
    55         for(int i=0;i<T;i++)
    56         {
    57             scanf("%d%d%d",&a,&b,&x);
    58              if(w[a][b] > x)w[a][b] = w[b][a] = x;
    59              key = MAX(key,MAX(a,b));
    60         }
    61 
    62         for(int i=0;i<S;i++)
    63         {
    64             scanf("%d", &s);
    65             w[0][s] = 0;
    66         }
    67         for(int i=0;i<D;i++)
    68         {
    69             scanf("%d",&t[i]);
    70         }
    71 
    72         dijkstra(0);
    73 
    74         int ans = INF;
    75         for(int j=0;j<D;j++)
    76         {
    77             ans = MIN(ans, d[t[j]]);
    78         }
    79         printf("%d
    ", ans);
    80 
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    查看线程
    shiro+多tomcat+redis实现session共享
    win11系统设置笔记本合盖上不休眠
    nvm切换node版本出现乱码 exit status 1:
    nvm安装vuecli
    SQL Server Management 2012 启动错误及解决:Cannot find one or more componets
    SQL Server 2012 连接 Oracle 11gR2 Database
    SQL Server 数据库跨区域时间问题
    SSIS 同步不同数据库的不同两张表
    Reporting Service 不能发送订阅报表的问题
  • 原文地址:https://www.cnblogs.com/gj-Acit/p/3204153.html
Copyright © 2011-2022 走看看