RE, Help
查看原帖
RE, Help
1054257
AndyCGM楼主2024/4/17 19:52
#include <iostream>
#include <cmath>
using namespace std;
int map[2566][2566];
int inf=0x3f3f3f3f;
int n,m,s,t;
int solve(int start){
    //Get ready.
    int result[2566],lastpoint[2566]={0},vis[2566]={0};
    for (int i=1; i<=n; i++)	result[i]=inf;
    result[start]=0;
    lastpoint[start]=start;
    //Start the main topic.
    for (int trytime=1; trytime<=n; trytime++){
        int sv=inf;//Smallest value
        int svi=0;//Snakkest value's index
        //Find the dot's ax vallue is smallest
        for (int index=1; index<=n; index++){
            if (result[index]<sv && vis[index]==0){
                sv=result[index];
                svi=index;
            }
        }
        int i=svi;
        for (int j=1; j<=n; j++){
            //If the dots i,j are connected dirrectly and i was unsettled.
            if (map[i][j]!=0 && vis[j]==0){
                //Caculate the Aproxed shortest way.
                int approx=result[i]+map[i][j];
                //If we find a better way, then we change the approx anwser.
                if (approx<result[j]){
                    result[j]=approx;
                    lastpoint[j]=i;    
                }
            }
        }
        //Settle this point
        vis[i]=1;
    }
    cout << result[t];
}

int main(){
    cin >> n >> m >> s >> t;
    for (int i=1; i<=m; i++){
        int u,v,w;
        cin >> u >> v >> w;
        if (map[u][v]==0){
            map[u][v]=w;
        }else{
            if (w<map[u][v]){
                map[u][v]=w;
            }
        }
    }
    solve(s);
    return 0;
}
2024/4/17 19:52
加载中...