代码随想录第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]);
    }
}
相关推荐
weixin_527550406 分钟前
初级程序员入门指南
javascript·python·算法
嘉陵妹妹2 小时前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon2 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
hn小菜鸡3 小时前
LeetCode 1356.根据数字二进制下1的数目排序
数据结构·算法·leetcode
zhuiQiuMX3 小时前
分享今天做的力扣SQL题
sql·算法·leetcode
music&movie4 小时前
算法工程师认知水平要求总结
人工智能·算法
laocui15 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910135 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
全栈凯哥6 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
全栈凯哥6 小时前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表