zoukankan      html  css  js  c++  java
  • hdu 2363(最短路+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2363

    思路:和之前hdu上做过的一题很像。。。就是先求求出所有的高度差,排序后枚举,每次都一次spfa,求出dist,若dist[n]!=inf,说明是在最小高度差下找到了最短路径,直接break即可。。。另外,发现若不用visited[]标记的话,时间是其两倍。。。orz....以后还是老老实实加visited [] 吧。。。

    View Code
     1 #include<iostream>
     2 #include<queue>
     3 #include<vector>
     4 #include<algorithm>
     5 const int MAXN=110;
     6 const int inf=1<<30;
     7 using namespace std;
     8 struct Node{
     9     int v,w;
    10 };
    11 vector<Node>mp[MAXN];
    12 struct Point{
    13     int low,high;
    14 }H[MAXN*MAXN];
    15 
    16 int h[MAXN];
    17 int dist[MAXN];
    18 bool visited[MAXN];
    19 int n,m;
    20 
    21 int cmp(const Point &p,const Point &q){
    22     return (p.high-p.low)<(q.high-q.low);
    23 }
    24 
    25 
    26 void SPFA(int low,int high){
    27     for(int i=1;i<=n;i++)dist[i]=inf;
    28     dist[1]=0;
    29     memset(visited,false,sizeof(visited));
    30     queue<int>Q;
    31     Q.push(1);
    32     while(!Q.empty()){
    33         int u=Q.front();
    34         Q.pop();
    35         visited[u]=false;//出队列要要置为false,因为有可能再次进队列
    36         if(h[u]<low||h[u]>high)continue;//起点要限制
    37         for(int i=0;i<mp[u].size();i++){
    38             int v=mp[u][i].v;
    39             int w=mp[u][i].w;
    40             //高度限制
    41             if(h[v]>=low&&h[v]<=high
    42                 &&dist[u]+w<dist[v]){
    43                     dist[v]=dist[u]+w;
    44                     if(!visited[v]){
    45                         Q.push(v);
    46                         visited[v]=true;
    47                     }
    48             }
    49         }
    50     }
    51 }
    52 
    53 int main(){
    54     int _case;
    55     scanf("%d",&_case);
    56     while(_case--){
    57         scanf("%d%d",&n,&m);
    58         for(int i=1;i<=n;i++)mp[i].clear();
    59         memset(H,0,sizeof(H));
    60         memset(h,0,sizeof(h));
    61         for(int i=1;i<=n;i++){
    62             scanf("%d",&h[i]);
    63         }
    64         for(int i=1;i<=m;i++){
    65             int u,v,w;
    66             scanf("%d%d%d",&u,&v,&w);
    67             Node p1,p2;
    68             p1.v=u,p2.v=v;
    69             p1.w=p2.w=w;
    70             mp[u].push_back(p2);
    71             mp[v].push_back(p1);
    72         }
    73         int k=0;
    74         for(int i=1;i<=n;i++){
    75             for(int j=i;j<=n;j++){
    76                 H[k].low=min(h[i],h[j]);
    77                 H[k++].high=max(h[i],h[j]);
    78             }
    79         }
    80         sort(H,H+k,cmp);
    81         int i=0;
    82         while(i<k){
    83             SPFA(H[i].low,H[i].high);
    84             if(dist[n]!=inf){
    85                 break;
    86             }
    87             i++;
    88         }
    89         printf("%d %d\n",H[i].high-H[i].low,dist[n]);
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    Leetcode 589. N-ary Tree Preorder Traversal
    Leetcode 912. Sort an Array
    Leetcode 1020. Number of Enclaves
    Leetcode 496. Next Greater Element I
    Leetcode 1019. Next Greater Node In Linked List
    Leetcode 503. Next Greater Element II
    Leetcode 1018. Binary Prefix Divisible By 5
    龟兔赛跑算法详解
    Leetcode 142. Linked List Cycle II
    Leetcode 141. Linked List Cycle
  • 原文地址:https://www.cnblogs.com/wally/p/3020010.html
Copyright © 2011-2022 走看看