zoukankan      html  css  js  c++  java
  • 数据结构学习第十九天

    15:54:06 2019-09-03

    学习

    PTA 第18题 利用Floyd的算法 解决多源最短路问题

    (os:读题没读懂 哎 语文理解力不行 )

      1 #define _CRT_SECURE_NO_WARNINGS  
      2 #include<stdio.h>
      3 #include<malloc.h>
      4 #define INIFITY 65635
      5 typedef struct ENode* Edge;
      6 struct ENode
      7 {
      8     int V1, V2;
      9     int Weight;
     10 };
     11 
     12 typedef struct Graph* MGraph;
     13 struct Graph
     14 {
     15     int Nv;
     16     int Ne;
     17     int G[200][200];
     18 };
     19 
     20 MGraph CreateGraph(int MaxVertex)
     21 {
     22     MGraph Graph = (MGraph)malloc(sizeof(struct Graph));
     23     Graph->Nv = MaxVertex;
     24     Graph->Ne = 0;
     25     for(int i=1;i<=Graph->Nv;i++)
     26         for (int j = 1; j <=Graph->Nv; j++)
     27         {
     28             if (i != j)
     29                 Graph->G[i][j] = INIFITY;
     30             else
     31                 Graph->G[i][j] = 0;
     32         }
     33     return Graph;
     34 }
     35 
     36 void Insert(MGraph Graph,Edge E)
     37 {
     38     Graph->G[E->V1][E->V2] = E->Weight;
     39     Graph->G[E->V2][E->V1] = E->Weight;
     40 }
     41 
     42 MGraph BuildGraph()
     43 {
     44     MGraph Graph;
     45     Edge E;
     46     int Nv;
     47     scanf("%d",  &Nv);
     48     Graph = CreateGraph(Nv);
     49     scanf("%d
    ",&Graph->Ne);
     50     for (int i = 0; i < Graph->Ne; i++)
     51     {
     52         E = (Edge)malloc(sizeof(struct ENode));
     53         scanf("%d %d %d
    ", &(E->V1), &(E->V2), &(E->Weight));
     54         Insert(Graph, E);
     55     }
     56     return Graph;
     57 }
     58 
     59 //多源最短路问题
     60 int Dist[200][200];
     61 int Path[200][200];
     62 void Floyd(MGraph Graph)
     63 {
     64     for(int i=1;i<=Graph->Nv;i++)
     65         for (int j = 1; j <= Graph->Nv; j++)
     66         {
     67             Dist[i][j] = Graph->G[i][j];
     68             Path[i][j] = -1;
     69         }
     70 
     71 
     72     for(int k=1;k<=Graph->Nv;k++)
     73         for(int i=1;i<=Graph->Nv;i++)
     74             for (int j = 1; j <= Graph->Nv; j++)
     75                 if (Dist[i][k] + Dist[k][j] < Dist[i][j])
     76                 {
     77                     Dist[i][j] = Dist[i][k] + Dist[k][j];
     78                     Path[i][j] = k;
     79                 }
     80 }
     81 void Judge(MGraph Graph)
     82 {
     83     int MaxChar[200] = { 0 };
     84     for (int i = 1; i <= Graph->Nv; i++)
     85         MaxChar[i] = Dist[i][1];
     86     for (int i = 1; i <=Graph->Nv; i++)
     87         for (int j = 1; j <= Graph->Nv; j++)
     88         {
     89             if (MaxChar[i] < Dist[i][j])
     90                 MaxChar[i] =Dist[i][j];
     91             if (Dist[i][j] == INIFITY)
     92             {
     93                 printf("%d", 0);
     94                 return;
     95             }
     96         }
     97 
     98     int Min= MaxChar[1];
     99     int Num=1;
    100     for (int i = 1; i <= Graph->Nv; i++)
    101     {
    102         if (MaxChar[i]< Min)
    103         {
    104             Min = MaxChar[i];
    105             Num = i;
    106         }
    107     }
    108     printf("%d %d", Num, Min);
    109 }
    110 
    111 int main()
    112 {
    113     MGraph Graph;
    114     Graph = BuildGraph();
    115     Floyd(Graph);
    116     Judge(Graph);
    117     return 0;
    118 }
    View Code

    PTA第19题 这道题没得全分数(不会做了...)

    而且写了好多冗杂的代码  各种变量随机定义 (不知道一个月后我还能不能看懂自己写的是什么)

      1 #define _CRT_SECURE_NO_WARNINGS  
      2 #include<stdio.h>
      3 #include<malloc.h>
      4 #include<math.h>
      5 #define True 1
      6 #define False 0
      7 #define SizeOfPosition 110
      8 #define INIFITY 65535
      9 float Length; //跳跃长度
     10 struct  Position
     11 {
     12     int x;
     13     int y;
     14 }Positions[SizeOfPosition];
     15 
     16 float Distance(float x1, float y1, float x2, float y2)
     17 {
     18     return sqrt(((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1)));
     19 }
     20 
     21 typedef struct ENode* Edge;
     22 struct ENode
     23 {
     24     int V1;
     25     int V2;
     26 };
     27 
     28 typedef struct Graph* MGraph;
     29 struct Graph
     30 {
     31     int Nv;
     32     int Ne;
     33     int G[110][110];
     34 };
     35 
     36 MGraph CreateGraph(int MaxVertex)
     37 {
     38     MGraph Graph = (MGraph)malloc(sizeof(struct Graph));
     39     Graph->Nv = MaxVertex;
     40     Graph->Ne = 0;
     41     for (int i = 0; i <=Graph->Nv; i++)
     42         for (int j = 0; j <=Graph->Nv; j++)
     43             Graph->G[i][j] = 0;
     44     return Graph;
     45 }
     46 
     47 void Insert(MGraph Graph, Edge E)
     48 {
     49     Graph->G[E->V1][E->V2] = 1;
     50     Graph->G[E->V2][E->V1] = 1;
     51 }
     52 
     53 MGraph BuildGraph()
     54 {
     55     MGraph Graph;
     56     Edge E;
     57     int Nv;
     58     scanf("%d %f
    ", &Nv, &Length);
     59     //printf("***%f
    ", Length);
     60     Graph = CreateGraph(Nv);
     61     Positions[0].x = 0;
     62     Positions[0].y = 0;
     63     for (int i =1; i <=Graph->Nv; i++)
     64     {
     65         int x, y;
     66         scanf("%d %d
    ", &x, &y);
     67         Positions[i].x = x;
     68         Positions[i].y = y;
     69     }
     70     for (int i =1; i <=Graph->Nv; i++)
     71         for (int j =i+1; j <=Graph->Nv; j++)
     72         {
     73             float dis = Distance(Positions[i].x, Positions[i].y, Positions[j].x, Positions[j].y);
     74             if (dis <= Length)
     75             {
     76                 E = (Edge)malloc(sizeof(struct ENode));
     77                 E->V1 = i;
     78                 E->V2 = j;
     79                 Insert(Graph, E);
     80             }
     81         }
     82     for (int j = 1; j <= Graph->Nv; j++)
     83     {
     84         float dis = Distance(Positions[0].x, Positions[0].y, Positions[j].x, Positions[j].y);
     85         if (dis <= (Length+7.5))
     86         {
     87             E = (Edge)malloc(sizeof(struct ENode));
     88             E->V1 = 0;
     89             E->V2 = j;
     90             Insert(Graph, E);
     91         }
     92     }
     93     return Graph;
     94 }
     95 int IsEdge(MGraph Graph, int V, int W)
     96 {
     97     return (Graph->G[V][W] == 1) ? 1 : 0;
     98 }
     99 
    100 #define Size 50
    101 int Queue[Size];
    102 int Front = 1;
    103 int Rear = 0;
    104 int size;
    105 
    106 int Succ(int Value)
    107 {
    108     if (Value < Size)
    109         return Value;
    110     else
    111         return 0;
    112 }
    113 int IsEmpty()
    114 {
    115     return (size == 0) ? 1 : 0;
    116 }
    117 int IsFull()
    118 {
    119     return (size == Size) ? 1 : 0;
    120 }
    121 void EnQueue(int Element)
    122 {
    123     Rear = Succ(Rear + 1);
    124     Queue[Rear] = Element;
    125     size++;
    126 }
    127 int DeQueue()
    128 {
    129     int Element = Queue[Front];
    130     Front = Succ(Front + 1);
    131     size--;
    132     return Element;
    133 }
    134 
    135 
    136 int visited[110];
    137 int could[110];  //记录能够跳出的节点
    138 int Has = 0; //记录是否有跳出的点
    139 int Judget(MGraph Graph, int V, float Length)  //利用深度优先遍历 计算可以跳出的点
    140 {
    141     visited[V] = 1;
    142     if (V == 0)
    143     {
    144         if (Distance(Positions[V].x, 50, Positions[V].x, Positions[V].y) <= (Length+7.5) || Distance(Positions[V].x, -50, Positions[V].x, Positions[V].y) <= (Length + 7.5)
    145             || Distance(50, Positions[V].y, Positions[V].x, Positions[V].y) <= (Length + 7.5) || Distance(-50, Positions[V].y, Positions[V].x, Positions[V].y) <= (Length + 7.5))
    146         {
    147             Has = 1;
    148             could[V] = 1;
    149         }
    150     }
    151     if (Distance(Positions[V].x, 50, Positions[V].x, Positions[V].y) <= Length || Distance(Positions[V].x, -50, Positions[V].x, Positions[V].y) <= Length
    152         || Distance(50, Positions[V].y, Positions[V].x, Positions[V].y) <= Length || Distance(-50, Positions[V].y, Positions[V].x, Positions[V].y) <= Length)
    153     {
    154         Has = 1;
    155         could[V] = 1;
    156     }
    157     for (int i = 0; i <=Graph->Nv; i++)
    158     {
    159         if (!visited[i] && IsEdge(Graph, V, i))
    160             Judget(Graph, i, Length);
    161     }
    162     return Has;
    163 }
    164 int Dist[110];   //源点V到W的最短路径
    165 int Path[110];
    166 
    167 void IntializeDistAndPath(MGraph Graph)
    168 {
    169     for (int i = 0; i < Graph->Nv; i++)
    170     {
    171         Dist[i] = -1;
    172         Path[i] = -1;
    173     }
    174 }
    175 void UnWeighted(MGraph Graph, int V, float Length)  // 利用单源最短路径算法  //计算最短路径
    176 {
    177     IntializeDistAndPath(Graph);
    178     EnQueue(V);
    179     Dist[V] = 0;
    180     while (!IsEmpty())
    181     {
    182         int W = DeQueue();
    183         for (int i = 0; i <= Graph->Nv; i++)
    184         {
    185             if (Dist[i] == -1 && IsEdge(Graph, W, i))
    186             {
    187                 Dist[i] = Dist[W] + 1;
    188                 Path[i] = W;
    189                 EnQueue(i);
    190             }
    191         }
    192     }
    193 }
    194 int Stack[110];
    195 int TopOfStack = 0;
    196 void Push(int num)
    197 {
    198     Stack[TopOfStack++] = num;
    199 }
    200 int Pop()
    201 {
    202     return Stack[--TopOfStack];
    203 }
    204 int Empty()
    205 {
    206     return (TopOfStack == 0) ? 1 : 0;
    207 }
    208 int Sum[110]; //收集跳数相同的
    209 void Print(MGraph Graph)
    210 {
    211     if(!Judget(Graph, 0, Length))
    212     {
    213         printf("0");
    214         return;
    215     }
    216     UnWeighted(Graph, 0, Length);
    217     int Min = INIFITY;  //记录最小路径
    218     int Num=-1;            //记录最小路径的下标
    219     float MinFirstJump = INIFITY;
    220     for (int i = 1; i <= Graph->Nv; i++)
    221     {
    222         if (could[i])
    223         {
    224             if (Dist[i] != -1 && Dist[i] <Min)
    225             {
    226                 Min = Dist[i];
    227                 Num = i;
    228             }
    229         }
    230     }
    231     for (int i = 1; i <110; i++)
    232     {
    233         if (could[i])
    234         {
    235             if (Dist[i] == Min)
    236             {
    237                 Sum[i] = i;
    238             }
    239         }
    240     }
    241     //计算每个元素的第一跳位置
    242     for (int i = 0; i < 110; i++)
    243     {
    244         if (Sum[i]!=0)
    245         {
    246             while (Path[Sum[i]] != 0)
    247             {
    248                 Sum[i] = Path[Sum[i]];
    249             }
    250         }
    251     }
    252     for (int i = 0; i < 110; i++)
    253     {
    254         if (Sum[i])
    255         {
    256             if (Distance(0, 0, Positions[Sum[i]].x, Positions[Sum[i]].y) < MinFirstJump)
    257             {
    258                 MinFirstJump = Distance(0, 0, Positions[Sum[i]].x, Positions[Sum[i]].y);
    259                 Num = i;
    260             }
    261         }
    262     }
    263     printf("%d
    ", Min+1);
    264     while(Num !=0)
    265     {
    266         Push(Num);
    267         Num = Path[Num];
    268     }
    269     while (!Empty())
    270     {
    271         Num = Pop();
    272         printf("%d %d
    ", Positions[Num].x, Positions[Num].y);
    273     }
    274 }
    275 int main()
    276 {
    277     MGraph Graph;
    278     Graph = BuildGraph();
    279     Print(Graph);
    280     return 0;
    281 }
    View Code
  • 相关阅读:
    Python YAML
    Python JASON
    Python正则表达式
    := / ?= /+=
    eclipse的springMVC环境搭建并输出HelloWorld
    SpringMVC简单介绍
    一个简单的MyBatis项目(应用)
    No mapping found for HTTP request with URI [/spring_liu/hello.do] in DispatcherServlet with name 'SpringMVC'
    xml中单词下面有提示下划线
    为什么maven 创建web工程不自动生成Deployment Descriptor:工程名
  • 原文地址:https://www.cnblogs.com/57one/p/11456109.html
Copyright © 2011-2022 走看看