zoukankan      html  css  js  c++  java
  • 传说中的次短路

    我不知道我是不是对了,没数据真是忧伤,我是传统的A*+SPFA算法,

    原题是求次短路,我借此学了一下这种神奇的东西。

      1 #include <iostream>
      2 #include <fstream>
      3 #include <cstdlib>
      4 #include <cstring>
      5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
      6 using namespace std;
      7 ifstream fin("secondpath.in");
      8 ofstream fout("secondpath.out");
      9 
     10 int places=0,rodes=0;
     11 int qi=0,zhong=0;
     12 int head[1005],cnt=0;
     13 struct id{
     14  int nxt;
     15  int to;
     16  int quan;    
     17 };
     18 id lian[200005];
     19 int dui[1003],jv[1003];
     20 int jv2[1003];
     21 bool used[1003];
     22 int you_dui[200005][2],gs_cnt=0;
     23 //int zhan[200003];
     24 
     25 
     26 void add(int co,int dao,int bian);
     27 void spfa( );
     28 void a_xin( );
     29 int na(int sze);
     30 void fang(int sze,int zhi); 
     31 
     32 
     33 void add(int co,int dao,int bian){
     34 lian[++cnt].nxt=head[co];    
     35 lian[cnt].to=dao;
     36 lian[cnt].quan=bian;
     37 head[co]=cnt;
     38 return;
     39 }
     40 
     41 
     42 void spfa( ){
     43 memset(jv,127/2,sizeof(jv));
     44 int tou=0,wei=1;
     45 dui[1]=zhong;jv[zhong]=0;    
     46 used[zhong]=1;
     47 while(tou!=wei){
     48  tou++;if(tou>1001)tou=1;
     49  int cong=dui[tou];
     50  for(int x=head[cong];x!=-1;x=lian[x].nxt){
     51   int dao=lian[x].to;
     52   if(jv[dao]>jv[cong]+lian[x].quan){
     53    jv[dao]=jv[cong]+lian[x].quan;    
     54    if(used[dao]==0){
     55     wei++;if(wei>1001)wei=1;
     56     dui[wei]=dao;used[dao]=1;
     57    }    
     58   }        
     59  }    
     60  used[cong]=0;    
     61 }
     62 return;
     63 }
     64 
     65 
     66 void a_xin( ){
     67 memset(you_dui,127/2,sizeof(you_dui));
     68 memset(jv2,127/2,sizeof(jv2));
     69 jv2[qi]=0;you_dui[1][0]=0;    
     70 you_dui[1][1]=qi;gs_cnt=1;
     71 int pd=0,shang=-1;
     72 while(pd<2&&gs_cnt!=0){
     73  int sze=you_dui[1][1];
     74  int jvli=na(1);
     75  //for(int x=1;x<=gs_cnt;x++)cout<<you_dui[x][0]<<" ";
     76 //  cout<<endl;
     77 //  system ("pause");
     78  if(sze==zhong&&shang!=jvli){pd++;shang=jvli;continue;}
     79  for(int hao=head[sze];hao!=-1;hao=lian[hao].nxt){
     80   int dao=lian[hao].to;
     81   if(jv2[sze]+lian[hao].quan!=jv2[dao])fang(dao,jv2[sze]+lian[hao].quan+jv[dao]);
     82   if(jv2[sze]+lian[hao].quan<jv2[dao]){jv2[dao]=jv2[sze]+lian[hao].quan;}  
     83  }
     84 // for(int x=1;x<=gs_cnt;x++)cout<<you_dui[x][0]<<" ";
     85 // cout<<endl;
     86 //  system ("pause");    
     87 }
     88 if(pd<2){cout<<"-1";fout<<"-1";return;}
     89 cout<<shang;fout<<shang;
     90 return;
     91 }
     92 
     93 
     94 int na(int sze){
     95 int a=you_dui[sze][1];
     96 you_dui[sze][1]=you_dui[gs_cnt][1];
     97 you_dui[gs_cnt][1]=a;
     98 a=you_dui[sze][0];you_dui[sze][0]=you_dui[gs_cnt][0];
     99 you_dui[gs_cnt][0]=a;gs_cnt--;
    100 int now=1,zi=0;
    101 while(now*2<=gs_cnt){
    102  zi=now*2;
    103  if(now*2<gs_cnt&&you_dui[zi+1][0]<you_dui[zi][0])zi++;
    104  if(you_dui[zi][0]>you_dui[now][0])return a;
    105  int b=you_dui[zi][0];you_dui[zi][0]=you_dui[now][0];
    106  you_dui[now][0]=b;b=you_dui[zi][1];
    107  you_dui[zi][1]=you_dui[now][1];you_dui[now][1]=b;
    108  now=zi;
    109 }    
    110 return a;    
    111 }
    112 
    113 
    114 void fang(int sze,int zhi){
    115 you_dui[++gs_cnt][1]=sze;    
    116 you_dui[gs_cnt][0]=zhi;    
    117 int now=gs_cnt,fu=0;
    118 while(now/2>0){
    119  fu=now/2;
    120  if(you_dui[fu][0]<you_dui[now][0])return;    
    121  int a=you_dui[now][0];you_dui[now][0]=you_dui[fu][0];
    122  you_dui[fu][0]=a;a=you_dui[now][1];you_dui[now][1]=you_dui[fu][1];
    123  you_dui[fu][1]=a;now=fu;    
    124 }    
    125 return;    
    126 }
    127 
    128 
    129 int main(int argc, char** argv) {
    130 fin>>places>>rodes;
    131 memset(head,-1,sizeof(head));
    132 for(int x=1;x<=rodes;x++){
    133 int a,b,c;
    134 fin>>a>>b>>c;
    135 add(a,b,c);
    136 add(b,a,c);    
    137 }    
    138 fin>>qi>>zhong;
    139 spfa( );
    140 a_xin( );
    141     
    142  return 0;
    143 }
  • 相关阅读:
    左萧龙(LZ)个人博客
    不同样式的计数
    CSS径向渐变radial-gradient
    优秀的Android资源
    读取csv格式的数据
    php 获取URL 各部分参数
    phpstorm查找替换文件中的变量
    PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置
    phpstorm 代码注释后,撤销某段代码的注释的,快捷键是什么?
    关于thinkphp5手动抛出Http异常时自定义404页面报错的问题
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5053446.html
Copyright © 2011-2022 走看看