3 条题解

  • 5
    @ 2025-7-2 16:07:06
    #include <bits/stdc++.h>
    using namespace std;
    
    int n, m;
    int s,t;
    int a[1010][1010];
    vector<int> adj[1010];
    
    int dis[1010];
    int vis[1010];
    
    int path[1010];
    
    struct Node {
        int v,d;
        bool operator < (const Node &a) const {
            return d > a.d;
        }
    };
    
    
    void dijkstra() {
    
        memset(dis, 0x3f, sizeof(dis));
        memset(vis, 0, sizeof(vis));
    
        priority_queue<Node> q;
        q.push({s, 0});
        dis[s] = 0;
    
        while (!q.empty()) {
            Node node = q.top();
            q.pop();
            int u = node.v;
            if (u == t) {
                break;
            }
            if (vis[u]) {
                continue;
            }
            vis[u] = 1;
            for (int i = 0;i < adj[u].size();++i) {
                int v = adj[u][i];
                int ndis = dis[u] + a[u][v];
                if (ndis < dis[v]) {
                    dis[v] = ndis;
                    path[v] = u;
                    q.push({v, ndis});
                }
            }
        }
    
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cin >> n >> m >> s >> t;
    
        for (int i = 0; i < m; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            if (a[u][v] == 0 || w < a[u][v]) {
                if (a[u][v] == 0) {
                    adj[u].push_back(v);
                    adj[v].push_back(u);
                }
                a[u][v] = w;
                a[v][u] = w;
            }
        }
    
        dijkstra();
    
        if (dis[t] == 0x3f3f3f3f) {
            cout << "can't arrive" << endl;
            return 0;
        }
        cout << dis[t] << endl;
    
        vector<int> ans;
    
        while (t ) {
            ans.push_back(t);
            t = path[t];
        }
    
        for (int i = ans.size() - 1; i >= 0; i--) {
            cout << ans[i] << " ";
        }
    
    
    
        return 0;
    }
    
    
    
    • 2
      @ 2025-7-2 16:26:20
      #include <bits/stdc++.h>
      using namespace std;
      
      int n, m;
      int s,t;
      
      
      struct Edge {
          int u, v, w;
      };
      
      vector<Edge> adj[1010];
      
      int dis[1010];
      int vis[1010];
      int path[1010];
      
      struct Node {
          int v,d;
          bool operator < (const Node &a) const {
              return d > a.d;
          }
      };
      
      
      void dijkstra() {
      
          memset(dis, 0x3f, sizeof(dis));
          memset(vis, 0, sizeof(vis));
      
          priority_queue<Node> q;
          q.push({s, 0});
          dis[s] = 0;
      
          while (!q.empty()) {
              Node node = q.top();
              q.pop();
              int u = node.v;
              if (u == t) {
                  break;
              }
              if (vis[u]) {
                  continue;
              }
              vis[u] = 1;
              for (int i = 0;i < adj[u].size();++i) {
                  Edge e = adj[u][i];
                  int ndis = dis[u] + e.w;
                  if (ndis < dis[e.v]) {
                      dis[e.v] = ndis;
                      path[e.v] = u;
                      q.push({e.v, ndis});
                  }
              }
          }
      
      }
      
      int main() {
          ios::sync_with_stdio(false);
          cin.tie(nullptr);
          cin >> n >> m >> s >> t;
      
          for (int i = 0; i < m; i++) {
              int u, v, w;
              cin >> u >> v >> w;
              adj[u].push_back({u, v, w});
              adj[v].push_back({v, u, w});
          }
      
          dijkstra();
      
          if (dis[t] == 0x3f3f3f3f) {
              cout << "can't arrive" << endl;
              return 0;
          }
          cout << dis[t] << endl;
      
          vector<int> ans;
      
          while (t ) {
              ans.push_back(t);
              t = path[t];
          }
      
          for (int i = ans.size() - 1; i >= 0; i--) {
              cout << ans[i] << " ";
          }
      
      
      
          return 0;
      }
      
      
      
      • -2
        @ 2025-7-2 16:07:27

        #include <bits/stdc++.h> using namespace std;

        int n, m; int s,t; int a[1010][1010]; vector adj[1010];

        int dis[1010]; int vis[1010];

        int path[1010];

        struct Node { int v,d; bool operator < (const Node &a) const { return d > a.d; } };

        void dijkstra() {

        memset(dis, 0x3f, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        
        priority_queue<Node> q;
        q.push({s, 0});
        dis[s] = 0;
        
        while (!q.empty()) {
            Node node = q.top();
            q.pop();
            int u = node.v;
            if (u == t) {
                break;
            }
            if (vis[u]) {
                continue;
            }
            vis[u] = 1;
            for (int i = 0;i < adj[u].size();++i) {
                int v = adj[u][i];
                int ndis = dis[u] + a[u][v];
                if (ndis < dis[v]) {
                    dis[v] = ndis;
                    path[v] = u;
                    q.push({v, ndis});
                }
            }
        }
        

        }

        int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> s >> t;

        for (int i = 0; i < m; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            if (a[u][v] == 0 || w < a[u][v]) {
                if (a[u][v] == 0) {
                    adj[u].push_back(v);
                    adj[v].push_back(u);
                }
                a[u][v] = w;
                a[v][u] = w;
            }
        }
        
        dijkstra();
        
        if (dis[t] == 0x3f3f3f3f) {
            cout << "can't arrive" << endl;
            return 0;
        }
        cout << dis[t] << endl;
        
        vector<int> ans;
        
        while (t ) {
            ans.push_back(t);
            t = path[t];
        }
        
        for (int i = ans.size() - 1; i >= 0; i--) {
            cout << ans[i] << " ";
        }
        
        
        
        return 0;
        

        }

        • 1

        信息

        ID
        2366
        时间
        1000ms
        内存
        32MiB
        难度
        7
        标签
        递交数
        16
        已通过
        9
        上传者