Skip to content

Pro4_WA_0

原始文件为 CPP 代码,本文是转换后的 Markdown 文件。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

const int INF = 10000;
const int maxn = 1005;
vector<int> adjG[maxn];
bool visit[maxn];
int edge[maxn][maxn];

void init(int n,int m);
int dfs(int des,int cur,int cur_ans,int result);
int main()
{
    freopen("in.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);

    init(n,m);
    //cout << "test" << endl;
    int answer = dfs(n,1,0,INF);
    cout << answer << endl;

    return 0;
}

void init(int n,int m)
{
    for (int i = 0; i < n; ++i)
    {
        adjG[i].clear();
        visit[i] = false;
    }
    for (int i = 0; i < m; ++i)
    {
        int from,to,weight;
        scanf("%d%d%d",&from,&to,&weight);
        adjG[from].push_back(to);
        adjG[to].push_back(from);
        edge[from][to] = weight;
        edge[to][from] = weight;
    }
}

int dfs(int des, int current, int cur_answer, int result)
{
    //cout << des << " " << current << " " << cur_answer << " " << result << endl;
    // current --- u
    int u = current;
    if (u == des)
    {
        return cur_answer;  
    }

    visit[u] = true;
    int adjnum = adjG[u].size();
    for (int i = 0; i < adjnum; ++i)
    {
        int v = adjG[u][i];
        if (result < edge[u][v] || visit[v]) continue;
        if (cur_answer < edge[u][v]) cur_answer = edge[u][v];

        int answer = dfs(des,v, cur_answer,result);
        if (result > answer)        
        {
            result = answer;
        }
    }
    visit[u] = false;
    return result;
}