zoukankan      html  css  js  c++  java
  • 图和树基础-蒜头君旅行

    https://www.jisuanke.com/course/2148/162482

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n;
     4 int m;
     5 /*
     6 通过邻接表对图存储
     7 */
     8 bool vis[20005];
     9 struct node
    10 {
    11     int v,w;//点和权值
    12 };
    13 vector<node> G[20005];
    14 void insert1(int u,int v,int w)
    15 {
    16     node temp;
    17     temp.v=v;
    18     temp.w=w;
    19     G[u].push_back(temp);
    20 }
    21 void insert2(int u,int v,int w)
    22 {
    23     insert1(u,v,w);
    24     insert1(v,u,w);
    25 }
    26 int u,v,w;
    27 bool f;
    28 int main()
    29 {
    30     cin>>n>>m;
    31     for(int i=0;i<m;i++)
    32     {
    33         cin>>u>>v>>w;
    34         insert2(u,v,w);
    35     }
    36     int j=0,minn=1000005,t=1;
    37     cout<<1;
    38     vis[1]=1;
    39     while(j!=t)
    40     {
    41     j=t;
    42     if(j!=1)
    43     cout<<" "<<j;
    44     minn=1000005;//每次循环对minn赋值
    45     for(int i=0;i<G[j].size();i++)
    46     {
    47         if(G[j][i].w<minn)
    48         {
    49             minn=G[j][i].w;
    50             t=G[j][i].v;
    51         }
    52     }
    53     if(vis[t]==1)//打标记避免找到重复的点
    54     break;//根据题意
    55     vis[t]=1;
    56   }
    57     return 0;
    58 }
  • 相关阅读:
    实验三 进程调度模拟程序
    实验二作业调度模拟程序实验报告
    实验8
    实验七
    实验六
    实验五 数独游戏界面设置
    实验五
    实验四
    实验三
    实验二
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/10630585.html
Copyright © 2011-2022 走看看