The code provided is an implementation of the Floyd-Warshall algorithm to find the shortest path between two nodes in a directed graph. However, the code seems to be stuck in an infinite loop because there is no termination condition for the while loop that finds the path between s1 and t1.

To fix this issue, you can add a termination condition to the while loop. One possible condition is to check if the current node is equal to t1. If it is, then the loop can be terminated.

Here's an updated version of the code with the termination condition added:

#include<bits/stdc++.h>
using namespace std;
inline static const nullptr_t _=[](){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr),cout.tie(nullptr);
    return nullptr;
}();
namespace fast_IO{
    #define FASTIO
    #define IOSIZE 100000
    char ibuf[IOSIZE],obuf[IOSIZE];char*p1=ibuf,*p2=ibuf,*p3=obuf;
    #define getchar() cin.get()
    #define putchar(x) cout.put(x)
    #define isspace(ch)(ch<33)
    template<typename T>inline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=(s<<3)+(s<<1)+(ch^48),ch=getchar();return s*w;}template<typename T>inline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=(s<<3)+(s<<1)+(ch^48),ch=getchar();return s*=w,true;}inline bool read(char&s){while(s=getchar(),isspace(s));return true;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return true;}template<typename T>inline void print(T x){if(x<0)putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}
    #ifdef _GLIBCXX_STRING
    inline bool read(string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))s+=ch,ch=getchar();return true;}inline void print(std::string x){for(int i=0,n=x.size();i<n;i++)putchar(x[i]);}
    #endif
    template<typename T,typename...T1>inline int read(T&a,T1&...other){return read(a)+read(other...);}template<typename T,typename...T1>inline void print(T a,T1...other){print(a);print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;template<typename T>Fast_IO&operator>>(Fast_IO&io,T&b){return read(b),io;}template<typename T>Fast_IO&operator<<(Fast_IO&io,T b){return print(b),io;}
}using namespace fast_IO;
constexpr int INF=1e9;
int s1,t1,s2,t2,n,m;
int main() {
    io>>s1>>t1>>s2>>t2>>n>>m;
    vector<vector<int>> graph(n+1, vector<int>(n+1, INF));
    for(int i=1;i<=n;i++) graph[i][i]=0;
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        graph[a][b]=c;
    }
    for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(graph[i][k]!=INF&&graph[k][j]!=INF&&graph[i][j]>graph[i][k]+graph[k][j]) graph[i][j]=graph[i][k]+graph[k][j];
    io<<graph[s1][t1]<<'\n';
    vector<int> path;
    path.emplace_back(s1);
    int cur=s1;
    while(cur!=t1){
        for(int i=1;i<=n;i++){
            if(graph[cur][i]!=INF&&graph[i][t1]!=INF&&graph[cur][i]+graph[i][t1]==graph[cur][t1]){
                path.emplace_back(i);
                cur=i;
                break;
            }
        }
        if (cur == t1) break; // Termination condition
    }
    for(int i=0;i<path.size();i++) io<<path[i]<<' ';
    cout.put(10);
    io<<graph[s2][t2]<<'\n';
    path.clear();
    path.emplace_back(s2);
    cur=s2;
    while(cur!=t2){
        for(int i=1;i<=n;i++){
            if(graph[cur][i]!=INF&&graph[i][t2]!=INF&&graph[cur][i]+graph[i][t2]==graph[cur][t2]){
                path.emplace_back(i);
                cur=i;
                break;
            }
        }
        if (cur == t2) break; // Termination condition
    }
    for(int i=0;i<path.size();i++) io<<path[i]<<' ';
    cout.put(10);
    return 0;
}

With this modification, the code should terminate properly and provide the desired output

#includebitsstdc++husing namespace std;inline static const nullptr_t _= ios_basesync_with_stdio0; cintienullptrcouttienullptr; return nullptr;;namespace fast_IO	#define FASTIO	#define IOSIZE

原文地址: http://www.cveoy.top/t/topic/hDie 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录