zoukankan      html  css  js  c++  java
  • poj1122 FDNY to the Rescue! 最短路

      这道题是最短路问题,开始卡了好久后来才发现是因为i,j坐标输入有问题。我更改了i,j坐标顺序才发现正确结果,坑啊!

      不过别的就是个裸地最短路了。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <algorithm>
     5 #define INF 100000000
     6 #define maxn 25
     7 using namespace std;
     8 int n;
     9 int edge[maxn][maxn];
    10 int dist[maxn];
    11 int s[maxn];
    12 int path[maxn];
    13 struct point
    14 {
    15     int num,len;
    16 }points[maxn];
    17 bool cmp(point a,point b)
    18 {
    19     return a.len<b.len;
    20 }
    21 void dijkstra(int v0)
    22 {
    23     int i,j,k;
    24     for(i=0;i<n;++i)
    25     {
    26         dist[i]=edge[v0][i];    s[i]=0;
    27         if(i!=v0&&dist[i]<INF)    path[i]=v0;
    28         else path[i]=1;
    29     }
    30     s[v0]=1;    dist[v0]=0;
    31     for(i=0;i<n-1;++i)
    32     {
    33         int min=INF,u=v0;
    34         for(j=0;j<n;++j)
    35         {
    36             if(!s[j]&&dist[j]<min)
    37             {
    38                 u=j;    min=dist[j];
    39             }
    40         }
    41         s[u]=1;
    42         for(k=0;k<n;++k)
    43         {
    44             if(!s[k]&&edge[u][k]<INF&&dist[u]+edge[u][k]<dist[k])
    45             {
    46                 dist[k]=dist[u]+edge[u][k];    path[k]=u;
    47             }
    48         }
    49     }
    50 }
    51 int main()
    52 {
    53     int i,j;
    54     cin>>n;
    55     for(i=0;i<n;++i)
    56         for(j=0;j<n;++j)
    57         {
    58             cin>>edge[j][i];
    59             if(edge[j][i]==-1) edge[j][i]=INF;
    60         }
    61     int begin,temp,count=0;
    62     cin>>begin;
    63     begin--;
    64     dijkstra(begin);
    65     while(cin>>temp)
    66     {
    67         if(temp==-2) break;
    68         points[count++].num=temp-1;
    69     }    
    70     for(i=0;i<count;++i)
    71         points[i].len=dist[points[i].num];
    72     sort(points,points+count,cmp);
    73     int shortest[maxn];
    74     cout<<"Org\tDest\tTime\tPath"<<endl;
    75     for(i=0;i<count;++i)
    76     {
    77         cout<<points[i].num+1<<"\t"<<begin+1<<"\t"<<points[i].len;
    78         memset(shortest,0,sizeof(shortest));
    79         int k=0;
    80         shortest[k]=points[i].num;
    81 
    82         while(path[shortest[k]]!=begin)
    83         {
    84             ++k;shortest[k]=path[shortest[k-1]];
    85         }
    86         k++;    shortest[k]=begin;
    87         if(shortest[k]==shortest[0]) cout<<"\t"<<shortest[k]+1;
    88         else
    89         for(j=0;j<=k;j++)
    90             cout<<"\t"<<shortest[j]+1;
    91         cout<<endl;
    92     }
    93 
    94     return 0;
    95 
    96 }
    97         
    98     
  • 相关阅读:
    sqlmap注入分类
    sqlmap简单中文说明
    【Python Learning第一篇】Linux命令学习及Vim命令的使用
    模拟退火算法从原理到实战【基础篇】
    平面上给定n条线段,找出一个点,使这个点到这n条线段的距离和最小。
    使用VMWareWorkstation10搭建学习环境笔记
    洛谷P1313 计算系数【快速幂+dp】
    浅析Numpy.genfromtxt及File I/O讲解
    持续交付中高效率与高质量
    持续集成CI与自动化测试
  • 原文地址:https://www.cnblogs.com/symons1992/p/2745051.html
Copyright © 2011-2022 走看看