POJ - 1797(求路径最小边最大值)

题目链接

题意:
求1 ~ n路径中最小边的最大值
思路:
dis[i]记录从1到达i点的最小边,最开始先将与1有边的点dis更新,
修改dijkstra,枚举n-1次,每次找出dis[i](即从1 ~ i点边的最小值)的最大的一个t,
用dis[t]更新其他点的dis -> max(dis[j], min(dis[t], a[t][j]));(关键一步)
感觉这题又是一种记录路径中的某些元素的最短路题

代码:

#include 
#include 
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> PII;

const int N = 1010;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;

int n, m;
bool vis[N];
int a[N][N], dis[N]; //dis[i]记录从1到达i点的最小边

void dijkstra()
{
    memset(vis, false, sizeof vis);
    memset(dis, 0, sizeof dis);
    for (int i = 1; i <= n; i++)
        dis[i] = max(dis[i], a[1][i]);
    vis[1] = true;
    for (int i = 1; i <= n - 1; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j] && (t == -1 || dis[j] > dis[t]))
                t = j;
        }
        vis[t] = true;
        for (int j = 1; j <= n; j++)
            dis[j] = max(dis[j], min(dis[t], a[t][j]));
    }
}

int main()
{
    fastio;
    int T;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        memset(a, 0, sizeof a);
        cin >> n >> m;
        while (m--)
        {
            int l, r, w;
            cin >> l >> r >> w;
            a[l][r] = a[r][l] = w;
        }
        dijkstra();
        cout << "Scenario #" << i << ':' << endl;
        cout << dis[n] << endl
             << endl; //题意每组样例之间要有一个空行
    }

    return 0;
}

你可能感兴趣的:(刷题记录,算法)