zoukankan      html  css  js  c++  java
  • poj1797

    简单题

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 1005

    int n, m;
    bool vis[maxn];
    int cost[maxn][maxn];
    int weight[maxn];

    void input()
    {
    scanf(
    "%d%d", &n, &m);
    memset(cost,
    0, sizeof(cost));
    for (int i = 0; i < m; i++)
    {
    int a, b, c;
    scanf(
    "%d%d%d", &a, &b, &c);
    a
    --;
    b
    --;
    cost[a][b]
    = c;
    cost[b][a]
    = c;
    }
    }

    int dijkstra()
    {
    int pre = 0;
    memset(vis,
    0, sizeof(vis));
    vis[
    0] = true;
    memset(weight,
    0, sizeof(weight));
    weight[
    0] = 0x3f3f3f3f;
    while (1)
    {
    for (int i = 0; i < n; i++)
    if (!vis[i] && min(weight[pre],cost[pre][i]) > weight[i])
    weight[i]
    = min(weight[pre],cost[pre][i]);
    int best = -1, besti = -1;
    for (int i = 0; i < n; i++)
    if (!vis[i] && weight[i] > best)
    {
    best
    = weight[i];
    besti
    = i;
    }
    if (besti == -1)
    break;
    vis[besti]
    = true;
    pre
    = besti;
    }
    return weight[n - 1];
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf(
    "%d", &t);
    for (int i = 0; i < t; i++)
    {
    input();
    int ans = dijkstra();
    printf(
    "Scenario #%d:\n%d\n\n", i + 1, ans);
    }
    return 0;
    }

  • 相关阅读:
    python命令行参数处理
    linux进程管理
    hadoop
    linux进程间通信之信号
    HA for openstack
    ubutun 安装php7.1x
    php 函数小技巧(一)
    git error: RPC failed; result=56, HTTP code = 200
    php面试题汇总四(基础篇附答案)
    php面试题汇总三(基础篇附答案)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2081724.html
Copyright © 2011-2022 走看看