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;
    }

  • 相关阅读:
    计算机网络协议如何学习之换位思考
    tcp/ip协议
    JSP页面中的元素
    动态网页脚本语言
    php的学习经验
    jsp、php和asp之间的区别
    Reactor 线程模型
    Hadoop集群搭建
    hadoop单点配置
    Docker 安装脚本
  • 原文地址:https://www.cnblogs.com/rainydays/p/2081724.html
Copyright © 2011-2022 走看看