代码随想录第59天

卡码网:47. 参加科学大会

python 复制代码
#include <iostream>
#include <unordered_set>
#include <queue>
#include <vector> 
#include <climits>

using namespace std;

struct cmp{
    bool operator ()(vector<int> a, vector<int> b) {
        return a[2] > b[2];
    } 
};

int main(){
    int n, m;
    cin>> n>> m;
    vector<vector<vector<int>>> graph(n+1);
    for(int i=0; i<m; i++){
        int start, end, len;
        cin>>start>>end>>len;
        graph[start].push_back({start, end,  len});
    }
    
    unordered_set<int> set;
    priority_queue<vector<int>, vector<vector<int>>, cmp> que;
    
    vector<int> length(n+1, INT_MAX);
    set.insert(1);
    for(int i=0; i<graph[1].size(); i++){
        if( set.find(graph[1][i][1]) == set.end() ){
            que.push(graph[1][i]);
            length[graph[1][i][1]] = graph[1][i][2];
        }
    }
    
    length[1] = 0;
    while( !que.empty() ){
        auto bian = que.top();
        if( set.find(bian[1]) != set.end() ){
            que.pop();
        } else {
            length[ bian[1] ] = min(length[bian[1]], length[bian[0]] + bian[2]);
            que.pop();
            set.insert(bian[1]);
            for(int i=0; i<graph[bian[1]].size(); i++){
                if( set.find(graph[bian[1]][i][1]) == set.end() ){
                    que.push(graph[bian[1]][i]);
                }
            }
        }
    }
    
    if( length[n] == INT_MAX) cout<<-1;
    else  cout<<length[n];
    
    return 0;
}

卡码网:94. 城市间货物运输 I

python 复制代码
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int bian= sc.nextInt();
        int [][]map=new int[n+1][n+1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(map[i],Integer.MAX_VALUE);
        }

        for (int i = 0; i < bian; i++) {
            int s1=sc.nextInt(),s2=sc.nextInt(),s3=sc.nextInt();
            map[s1][s2]=s3;
        }
        int[] res = new int[n + 1];
        Arrays.fill(res,Integer.MAX_VALUE);
        res[1]=0;
        for(int k=0;k<n;k++){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if(map[i][j]!=Integer.MAX_VALUE&&res[i]!=Integer.MAX_VALUE){
                    if(res[i]+map[i][j]<res[j]){
                        res[j]=res[i]+map[i][j];
                    }
                }
            }
        }}
        System.out.println(res[n]==Integer.MAX_VALUE?"unconnected":res[n]);
    }
}
相关推荐
一次旅行40 分钟前
AutoAWQ完整实战:MIT激活感知AWQ量化,模型显存减半、推理提速且精度无损
人工智能·python·算法
GrowthDiary0071 小时前
算法题:寻找二维数组top k问题
数据结构·python·算法
可编程芯片开发1 小时前
基于全阶观测器的三自由度运动系统状态反馈控制simulink建模与仿真
算法
kobesdu1 小时前
从零推导FAST-LIO的观测雅可比矩阵
人工智能·算法·矩阵
乐思智能科技有限公司2 小时前
PLECS软件学习使用(二)直流电机基本系统模型
人工智能·算法·机器学习·面试·职场和发展
随意起个昵称2 小时前
贪心模型-Johnson法则
c++·算法
旋生万物2 小时前
电磁力 = 螺旋联络的 90° 旋转?麦克斯韦方程组的几何重构
人工智能·python·算法·机器学习·copilot·世界模型·物理ai
余俊晖3 小时前
多模态大模型细粒度视觉理解:Vision-OPD在线策略自蒸馏技术方案概述
人工智能·深度学习·算法·多模态·opd
脚踏实地皮皮晨3 小时前
003003001_Grid控件
开发语言·windows·算法·c#·visual studio
Hi李耶3 小时前
【LeetCode】6-Z字形变换
算法·leetcode·职场和发展