zoukankan      html  css  js  c++  java
  • 邻接表解决多段图问题

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <math.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <stdlib.h>
      7 using namespace std;
      8 const int MAXN=100;
      9 struct Lis
     10 {
     11     int id;
     12     int dis;
     13     Lis * next;
     14 };
     15 struct Num
     16 {
     17     int src_dis;
     18     Lis * head;
     19 }num[MAXN];
     20 
     21 int stage[10][100];
     22 void init(int n,int k)//初始化数据;
     23 {
     24     int m;
     25     memset(num,0,sizeof(num));
     26     for (int i = 1; i <= n; ++i)
     27     {
     28         num[i].src_dis=10000;
     29         num[i].head=(Lis *)malloc(sizeof(Lis));
     30         num[i].head->next=NULL;
     31     }
     32     int d1,d2,distance;
     33     Lis *p;
     34     cout<<"输入边的长度:输入1 2 4 表示点1 与 2的边的长度为 4:首数字为0表示结束输入."<<endl;
     35     while(1)
     36     {
     37         cin>>d1;
     38         if(d1==0)break;
     39         cin>>d2>>distance;
     40 
     41         p=(Lis *)malloc(sizeof(Lis));
     42         p->id=d2;p->dis=distance;
     43         if(num[d1].head->next==NULL)
     44         {
     45             num[d1].head->next=p;
     46             p->next=NULL;
     47         }
     48         else{
     49             p->next=num[d1].head->next;
     50             num[d1].head->next=p;
     51         }
     52         
     53     }
     54     cout<<"输入阶段"<<endl;
     55  
     56 
     57     memset(stage,-1,sizeof stage);//若为-1  则无数据了
     58     num[1].src_dis=0;
     59     for(int i=1;i<=k;i++)
     60     {
     61         cin>>stage[i][0];    //第i个阶段的[0]是存当前阶段的个数
     62 
     63         for(int j=1;j<=stage[i][0];j++)
     64         {
     65             cin>>stage[i][j];
     66         }
     67     }
     68 
     69 
     70 
     71 
     72 }
     73 
     74 void deal(int n,int k)
     75 {
     76     int i,j;
     77     int a,b,c;
     78     num[stage[1][1]].src_dis=0;//第一阶段的数到自己的距离
     79 
     80 
     81 
     82 
     83     for (i = 2; i <= k; ++i)
     84     {
     85         if(i==2)
     86         {
     87             a=stage[1][1];
     88             Lis *p=num[a].head->next;
     89             for (j = 1; j <= stage[i][0]; ++j)
     90             {
     91                 //while(p.id==a)
     92                 b=p->id;c=p->dis;
     93                 num[b].src_dis=c;
     94                 p=p->next;
     95 
     96             }
     97         }
     98         else
     99         {
    100             for(int k = 1;k<=stage[i-1][0];k++)
    101             {
    102                 int tmp=stage[i-1][k];
    103                 cout<<"tmp="<<tmp<<endl;
    104                 a=tmp;
    105                     Lis *p=num[a].head->next;
    106                 for (j = 1; j <= stage[i][0]; ++j)
    107                 {
    108                     
    109                     b=p->id;c=p->dis+num[a].src_dis;//b为当前的点
    110                     //判断下第b个点的最短到达的数
    111 
    112                     num[b].src_dis=min(c,num[b].src_dis);
    113                     p=p->next;
    114                     if(!p)break;
    115                 }
    116 
    117             }
    118 
    119 
    120     // for(int x=1;x<=n;x++)
    121     // {
    122     //     cout<<"stage "<<x<<" "<<num[x].src_dis<<"  ";
    123     // }
    124     // cout<<endl<<endl;
    125 
    126 
    127             
    128         }
    129 
    130         
    131 
    132 
    133     }
    134     int las = stage[k][1];
    135     //cout<<"world"<<endl;
    136     cout<<"dis = "<<num[las].src_dis<<endl;
    137 }
    138 int main()
    139 {
    140     int n,k;
    141     cout<<"输入数据个数,和阶段数:";
    142     cin>>n>>k;
    143     init(n,k);
    144     
    145     deal(n,k);
    146 
    147 
    148 
    149 
    150 
    151     return 0;
    152 }
    153 
    154 
    155 /*
    156 
    157 12
    158 5
    159 1 2 9
    160 1 3 7
    161 1 4 3
    162 1 5 2
    163 2 6 4
    164 2 7 2
    165 2 8 1
    166 3 6 2
    167 3 7 7
    168 4 8 11
    169 5 7 11
    170 5 8 8
    171 6 9 6
    172 6 10 5
    173 7 9 4
    174 7 10 3
    175 8 10 5
    176 8 11 6
    177 9 12 4
    178 10 12 2
    179 11 12 5
    180 0
    181 1
    182 1
    183 4
    184 2 3 4 5
    185 3
    186 6 7 8
    187 3
    188 9 10 11
    189 1
    190 12
    191 
    192 
    193 4 3
    194 1 2 3
    195 1 3 4
    196 2 4 5
    197 3 4 6
    198 0
    199 
    200 
    201 */
  • 相关阅读:
    [WPF]Win10便签软件
    [WPF]使用Fody提高效率
    [WPF]限制程序单例运行
    [WPF]创建系统栏小图标
    Run Performance Testing Which Was Distributed To Multiple Test Agents
    FxZ,C#开发职位面试测试题(30分钟内必须完成)
    BYS推荐MS前端PhoneCall面试问题整理-2
    BYS推荐MS前端PhoneCall面试问题整理-1
    LxNx前端F2F面试问题整理
    复杂DIV交错布局
  • 原文地址:https://www.cnblogs.com/sylvialucy/p/5132492.html
Copyright © 2011-2022 走看看