poj 1502 单源最短路径

简介:

一、题目大意

无向图,给出邻接矩阵的下半矩阵,要求源点1,到其他点最短时间(散播整个网络的最短时间)。

二、AC code

明显的单源最短路径

但是还是用了Floyd算法撞撞运气,毕竟是无向图,当然可以对Floyd优化,最后也可以A。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
//#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 102
int map[MAXN][MAXN];

int main()
{
    //freopen("input.txt","r",stdin);

    int n;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {

            string s;
            cin>>s;

            int tmp = INF;
            if(s != "x"){
                tmp = stringToNum<int > (s);
            }

            map[j][i] = map[i][j] = tmp;
        }
    }

    //floyd
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j < i; ++j) {

                map[j][i] = map[i][j] = min(map[i][j],map[i][k]+map[k][j]);
            }
        }
    }

    int time = -1;
    for (int i = 2; i <= n ; ++i) {
        time = time > map[1][i]? time : map[1][i];
    }
    cout<<time<<endl;

    return 0;
}

dijkstra:

重定向忘记删掉,WA一次,甚是可惜

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
//#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 102
int map[MAXN][MAXN];
bool visited[MAXN];

void update(int minPos,int n) {

    for (int i = 1; i <= n; ++i) {

        if( !visited[i] && map[1][i] > map[1][minPos] + map[minPos][i] )
            map[i][1] = map[1][i] = map[1][minPos] + map[minPos][i];
    }
}

void dijkstra(int n) {

    visited[1] = 1;
    while(1) {

        int tmpMin = INF;
        int minPos = -1;
        for (int i = 2; i <= n; ++i) {

            if( !visited[i] && tmpMin > map[1][i] ) {
                //have not been visited && smaller

                tmpMin = map[1][i];
                minPos = i;
            }
        }

        if( -1 == minPos )
            break;

        visited[minPos] = 1;
        update(minPos,n); //update the map, if it's shorter to get through minPos
    }
}

int main()
{
    //freopen("input.txt","r",stdin);

    int n;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {

            string s;
            cin>>s;

            int tmp = INF;
            if(s != "x"){
                tmp = stringToNum<int > (s);
            }

            map[j][i] = map[i][j] = tmp;
        }
    }

    dijkstra(n);

    int time = -1;
    for (int i = 2; i <= n ; ++i) {
        time = time > map[1][i]? time : map[1][i];
    }
    cout<<time<<endl;

    return 0;
}
相关文章
支付宝境外(海外)问题方案集锦
说明:   境外和境内接口不同签约等不同,如果要接入境外请按照境外方式接入 。   境外接口官网:[url]https://global.alipay.com[/url] 声明:  业务和技术方案可能会有变动最终以境外业务和技术支持回复为准,下面问题仅供参考。
7215 12
|
3月前
|
人工智能 JSON Shell
把 AI 漫剧做成一条可复现的生产管线:百炼 CLI 四步链路与单价账
本文聚焦AI漫剧工业化生产,以百炼CLI(v1.22.0)构建可复现、可验算、可计价的四步管线:角色设定图→参考图分镜→配音→生视频。强调“参考图”为唯一身份锚点,破除seed误区;提供实测命令、耗时与单价(单集约¥3.8),附完整可执行脚本,兼顾成本治理与合规边界。(239字)
把 AI 漫剧做成一条可复现的生产管线:百炼 CLI 四步链路与单价账
|
3月前
|
JavaScript
DSH plugin 怎么写:从 apply 与 ctx 的关系、四种常见能力的代码写法,到新手最容易写错的三个地方
DeepSeek Harness(DSH)插件怎么写:先理解插件就是导出 apply 的模块、能力都在 apply 里通过 ctx 注册,再照四段代码把工具、配置、依赖、事件写进去,最后避开混写导出、漏写 inject、忘记清理这三个高频错误。
376 0
|
3月前
|
JavaScript 开发工具 git
DSH plugin 开发环境搭建:Node 与 pnpm 准备、两种获取 Harness 的方式、工程依赖与脚手架
DeepSeek Harness(DSH)插件开发环境搭建:装 Node 与 pnpm,用 npx @deepseek-ai/dsh 或源码检出获得 Harness,再用 create-dsh-plugin 生成工程,装好 cordis、dsh-tools、schemastery 三类依赖。
690 0
|
3月前
|
存储 人工智能 弹性计算
阿里云服务器一年多少钱?整理2026年最新爆款服务器配置排行榜,共40台!
2026阿里云服务器40款爆款配置排行榜,覆盖个人入门、企业主力、AI算力及出海业务四大场景,含轻量应用、ECS、GPU实例等,价格年付38元起,附选购建议与节点推荐。(239字)
528 0
|
4月前
|
存储 运维 安全
医疗内网纵深防御安全体系实战方案
本文剖析医疗内网“终端失控、边界模糊、数据泄露”三大痛点,结合等保2.0与《数据安全法》要求,提出以身份为核心、数据为资产的纵深防御方案:涵盖网络准入控制、终端全生命周期管理、安全数据交换、外设精细化管控等闭环措施,兼顾业务连续性与合规达标。
|
4月前
|
数据采集 缓存 监控
taobao.item.search_img(淘宝拍立淘图片搜索 API)全业务场景落地手册
taobao.item_search_img(拍立淘API)是阿里官方视觉检索接口,支持图片URL/Base64输入,在淘宝/天猫库中精准匹配同款或相似商品,返回结构化数据及相似度得分。广泛应用于跨境选品、货源溯源、ERP上架、竞品监控等10大场景,可联动商品详情、评论等接口构建完整电商数据链路。(239字)
|
5月前
|
存储 人工智能 运维
让 Agent 越用越准、成本越来越低:AgentLoop 的 Agent 经验自进化闭环
企业 Agent 上线后,如何在不重新训练模型的情况下提升准确率、稳定性并优化单位成功成本?本文介绍 AgentLoop 如何基于真实运行轨迹自动生成可复用经验,通过 Skill 和 CLI 按需召回,并给出控制台接入步骤及与 Memory、RAG、微调、RL 的差异。
832 1
|
5月前
|
数据采集 Web App开发 JavaScript
全网电影信息爬取:从单机脚本到分布式采集系统的工程实践
全网电影信息爬取:从单机脚本到分布式采集系统的工程实践
|
12月前
|
敏捷开发 人工智能 监控
AI 正在“杀死”敏捷开发?别闹了,它反而让我们重新读懂敏捷的真谛
AI时代,敏捷开发非但不会消亡,反而迎来重生。它戳破伪敏捷的泡沫,倒逼团队回归“以人为本、快速验证价值”的初心。AI替代不了人的洞察与判断,只会让真正的敏捷更珍贵。