• German Collegiate Programming Contest 2018​


     1 //   Coolest Ski Route
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <string>
     6 #include <algorithm>
     7 #include <utility>
     8 #include <vector>
     9 #include <map>
    10 #include <queue>
    11 #include <stack>
    12 #include <cstdlib>
    13 #include <cmath>
    14 typedef long long ll;
    15 #define lowbit(x) (x&(-x))
    16 #define ls l,m,rt<<1
    17 #define rs m+1,r,rt<<1|1
    18 using namespace std;
    19 #define pi acos(-1)
    20 typedef pair<int,int> P;
    21 const int N=1100;
    22 const int inf=0x3f3f3f3f;
    23 int dis[N];
    24 int n,m;
    25 struct Node{
    26     int to,w;
    27     Node(){}
    28     Node(int TO,int W){
    29         to=TO;
    30         w=W;
    31     }
    32 };
    33 vector<Node>vec[N];
    34 void bfs(){
    35     priority_queue<P,vector<P>,less<P> >que;//队首元素为最长距离
    36     for(int i=1;i<=n;i++){
    37         que.push(P(0,i));//起初,只有自己到自己
    38     }
    39     while(!que.empty()){
    40         P q=que.top();
    41         que.pop();
    42         int v=q.second;
    43         if(dis[v]>q.first) continue;//比当前的还大,就不用继续往下找了
    44         for(int i=0;i<vec[v].size();i++){
    45             Node Nod=vec[v][i];
    46             int t=Nod.to;
    47             if(dis[t]<dis[v]+Nod.w){
    48                 dis[t]=dis[v]+Nod.w;//不断更新dis[t]
    49                 que.push(P(dis[t],t));
    50             }
    51         }
    52     }
    53 }
    54 int  main()
    55 {
    56     scanf("%d%d",&n,&m);
    57     int s,t,c;
    58     for(int i=0;i<m;i++)
    59     {
    60         scanf("%d%d%d",&s,&t,&c);
    61         vec[s].push_back(Node(t,c));//有向图
    62     }
    63     memset(dis,0,sizeof(dis));//要初始化为0
    64     bfs();
    65     int MAX=-inf;
    66     for(int i=1;i<=n;i++){
    67         MAX=max(MAX,dis[i]);//i为终点,dis[i]为到i的最大路径长度
    68     }
    69     printf("%d
    ",MAX);
    70     return 0;
    71 }
     1 //   Coolest Ski Route 
     2 #include <bits/stdc++.h>
     3 #define ll long long
     4 using namespace std;
     5 const int N = 1e3+10;
     6 struct Node{
     7     int  to,w;
     8     Node(){}
     9     Node(int TO,int W){
    10         to=TO;
    11         w=W;
    12     }
    13 };
    14 vector<Node> vs[N];
    15 int d[N],in[N];
    16 void dfs(int v) {
    17     for(int i = 0; i < vs[v].size(); i ++) {
    18         Node Nod =vs[v][i];
    19         int u=Nod.to;
    20         if(d[u] < d[v] + Nod.w ) {
    21             d[u] = d[v] + Nod.w;
    22             dfs(u);
    23         }
    24     }
    25 }
    26 int main() {
    27     int n, m;
    28     cin >> n >> m;
    29     for(int i = 0; i < m; i ++) {
    30         int u, v, w;
    31         cin >> u >> v >> w;
    32         vs[u].push_back(Node(v,w));
    33         in[v]++;
    34     }
    35     for(int i = 1; i <= n; i ++) {
    36         if(!in[i])//本题任意两点之间为单向
    37         dfs(i);
    38     }
    39     int MAX = 0;
    40     for(int i = 1; i <= n; i ++) MAX = max(MAX, d[i]);
    41     printf("%d
    ",MAX);
    42     return 0;
    43 }
  • 相关阅读:
    HDFS上传文件
    SparkStreaming和Kafka的整合
    hadoop ha集群搭建
    Zookeeper安装
    hadoop完全分布式集群的搭建
    hadoop伪分布式集群的搭建
    java笔记之字符串,gc
    java笔记之字符串,gc
    java基础之short案例分析以及final关键字
    java基础之short案例分析以及final关键字
  • 原文地址:https://www.cnblogs.com/tingtin/p/9338034.html
Copyright © 2020-2023  润新知