• Dijkstr优化(Hide and Seek)


    Hide and Seek

    • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
    • Total Submission(s): 24     Accepted Submission(s): 0
    Description

    Bessie is playing hide and seek (a game in which a number of players hide and a single player (the seeker) attempts to find them after which various penalties and rewards are assessed; much fun usually ensues).

    She is trying to figure out in which of N (2 ≤ N ≤ 20,000) barns conveniently numbered 1..N she should hide. She knows that FJ (the seeker) starts out in barn 1. All the barns are connected by M (1 ≤ M ≤ 50,000) bidirectional paths with endpoints Ai and Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ NAi ≠ Bi); it is possible to reach any barn from any other through the paths.

    Bessie decides that it will be safest to hide in the barn that has the greatest distance from barn 1 (the distance between two barns is the smallest number of paths that one must traverse to get from one to the other). Help Bessie figure out the best barn in which to hide.

    Input

    Multiple test cases. For each case:

    * Line 1: Two space-separated integers: N and M

    * Lines 2..M+1: Line i+1 contains the endpoints for path iAi and Bi

    Output

    For each case :

    * Line 1: On a single line, print three space-separated integers: the index of the barn farthest from barn 1 (if there are multiple such barns, print the smallest such index), the smallest number of paths needed to reach this barn from barn 1, and the number of barns with this number of paths.

    Sample Input

    6 7
    3 6
    4 3
    3 2
    1 3
    1 2
    2 4
    5 2

    Sample Output

    4 2 3

    Hint

    The farm layout is as follows:

    1--2--5

     | /|

     |/ |
    3--4
     |   
     6  

    Barns 4, 5, and 6 are all a distance of 2 from barn 1. We choose barn 4 because it has the smallest index.

    以下为错误代码(手动滑稽),用了dijkstr.

    首先是没有邻接表优化的,后面一个是邻接表优化的,第一个访问越界,第二个超时(qwq,想哭,打算用spaf再肝一次)

    用优先队列优化

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define INF 1<<30
     4 #define man 20006
     5 int v,e;
     6 int mp[man][man];
     7 int dist[man];
     8 bool flag[man];
     9 typedef pair <int ,int >P;
    10 void init()
    11 {
    12     for(int i=0;i<=v;i++){
    13         for(int j=0;j<=v;j++)
    14             mp[i][j]=INF;
    15         dist[i]=INF;
    16     }
    17     memset( flag, false, sizeof flag);
    18 }
    19 
    20 void Dijkstra(int s)
    21 {
    22     dist[s]=0;
    23     priority_queue<P,vector<P>,greater<P> > q;
    24     q.push(P(0,s));
    25     while(!q.empty()){
    26         P p=q.top();
    27         q.pop();
    28         int vi=p.second;
    29         if(flag[vi])
    30             continue;
    31         flag[vi]=true;
    32         for(int i=1;i<=v;i++){
    33             if(!flag[i]&&dist[i]>dist[vi]+mp[i][vi]){
    34                 dist[i]=dist[vi]+mp[i][vi];
    35                 q.push(P(dist[i],i));
    36             }
    37         }
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     while( ~scanf("%d%d",&v,&e)){
    44         init();
    45         for(int i=0;i<e;i++){
    46             int x,y;
    47             scanf("%d%d",&x,&y);
    48             mp[x][y]=mp[y][x]=1;
    49         }
    50         Dijkstra(1);
    51     }
    52     return 0;
    53 }
    View Code

     Dijkstr优先队列与邻接表

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define INF 1e7
     4 #define man 20006
     5 int v,e,num;
     6 int dist[man],y[man],len[man],nex[man],fir[man];
     7 bool flag[man];
     8 typedef pair <int ,int >P;
     9 void init()
    10 {
    11     for(int i=0;i<=v;i++){
    12         dist[i]=INF;
    13     }
    14     memset( fir, -1, sizeof fir);
    15     memset( y, 0, sizeof y);
    16     memset( flag, false, sizeof flag);
    17 }
    18 
    19 void Dijkstra(int s)
    20 {
    21     dist[s]=0;
    22     priority_queue<P,vector<P>,greater<P> > q;
    23     q.push(P(0,s));
    24     while(!q.empty()){
    25         P p=q.top();
    26         q.pop();
    27         int vi=p.second;
    28         if(flag[vi])
    29             continue;
    30         flag[vi]=true;
    31         for(int i=fir[vi];i!=-1;i=nex[i]){
    32             if(!flag[y[i]]&&dist[y[i]]>dist[vi]+1){
    33                 dist[y[i]]=dist[vi]+1;
    34                 q.push(P(dist[y[i]],y[i]));
    35             }
    36         }
    37     }
    38 }
    39 
    40 void add(int a1,int b1)
    41 {
    42     y[num]=b1;
    43     nex[num]=fir[a1];
    44     fir[a1]=num;
    45     len[num]=1;
    46     num++;
    47 }
    48 
    49 int main()
    50 {
    51     while( ~scanf("%d%d",&v,&e)){
    52         init();
    53         num=1;
    54         for(int i=1;i<=e;i++){
    55             int a1,b1;
    56             scanf("%d%d",&a1,&b1);
    57             add(a1,b1);
    58             add(b1,a1);
    59         }
    60         Dijkstra(1);
    61         int an1=INF,an2=-10,an3=0;
    62         for(int i=1;i<=v;i++){
    63             if(dist[i]>an2)
    64                 an2=dist[i];
    65         }
    66         for(int i=1;i<=v;i++){
    67 //            printf("%d ",dist[i]);
    68             if(dist[i]==an2){
    69                 an1=min(an1,i);
    70                 an3++;
    71             }
    72         }
    73         printf("%d %d %d
    ",an1,an2,an3);
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    JDK安装与配置
    MAVEN安装与配置
    http协议与soap协议之间的区别
    Zookeeper
    TCP/IP及http协议 SOAP REST
    Java数据结构总述
    SQL基础
    IntelliJ IDEA 快捷键
    反射机制
    java集合的作用
  • 原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/8664051.html
Copyright © 2020-2023  润新知