代码随想录第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]);
    }
}
相关推荐
向阳@向远方2 小时前
第二章 简单程序设计
开发语言·c++·算法
github_czy2 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁3 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子3 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z4 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao4 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx4 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背4 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z4 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼5 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode