• 【HDOJ】4109 Instrction Arrangement


    差分约束。

     1 /* 4109 */
     2 #include <iostream>
     3 #include <queue>
     4 #include <vector>
     5 #include <algorithm>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <cstdlib>
     9 #include <climits>
    10 using namespace std;
    11 
    12 #define MAXV 1005
    13 #define MAXE 41005
    14 #define INF  0x3f
    15 
    16 typedef struct {
    17     int v, w, next;
    18 } Edge_t;
    19 
    20 Edge_t E[MAXE];
    21 int head[MAXV], L;
    22 int dis[MAXV];
    23 bool visit[MAXV];
    24 int n, m;
    25 
    26 void init() {
    27     for (int i=0; i<=n; ++i)
    28         dis[i] = INT_MIN;
    29     memset(head, -1, sizeof(int)*(n+1));
    30     memset(visit, false, sizeof(bool)*(n+1));
    31     L = 0;
    32 }
    33 
    34 void addEdge(int u, int v, int w) {
    35     E[L].v = v;
    36     E[L].w = w;
    37     E[L].next = head[u];
    38     head[u] = L++;
    39 }
    40 
    41 void spfa() {
    42     int i, j, k;
    43     int u, v, w;
    44     queue<int> Q;
    45 
    46     visit[n] = true;
    47     dis[n] = 0;
    48     Q.push(n);
    49 
    50     while (!Q.empty()) {
    51         u = Q.front();
    52         Q.pop();
    53         visit[u] = false;
    54         for (i=head[u]; i!=-1; i=E[i].next) {
    55             v = E[i].v;
    56             w = E[i].w;
    57             if (dis[v] < dis[u]+w) {
    58                 dis[v] = dis[u] + w;
    59                 if (!visit[v]) {
    60                     visit[v] = true;
    61                     Q.push(v);
    62                 }
    63             }
    64         }
    65     }
    66 }
    67 
    68 int main() {
    69     int i, j, k;
    70 
    71     #ifndef ONLINE_JUDGE
    72         freopen("data.in", "r", stdin);
    73         freopen("data.out", "w", stdout);
    74     #endif
    75 
    76     while (scanf("%d %d",&n,&m) != EOF) {
    77         init();
    78         while (m--) {
    79             scanf("%d%d%d", &i, &j, &k);
    80             addEdge(j, i, k);
    81         }
    82         for (i=0; i<n; ++i)
    83             addEdge(n, i, 0);
    84         spfa();
    85         k = INT_MIN;
    86         for (i=0; i<=n; ++i)
    87             k = max(k, dis[i]);
    88         printf("%d
    ", k+1);
    89     }
    90 
    91     return 0;
    92 }
  • 相关阅读:
    [POI2013]BAJ-ytecomputer [动态规划]
    【2019.10.15】网课 Za
    【初赛】
    [NOI2014]魔法森林[最短路 spfa]
    【洛谷2019金秋营模拟赛1】
    【luogu1315】 观光公交[贪心]
    【luogu4450】收集邮票 [期望dp]
    [HAOI2012]高速公路 [线段树 期望]
    ALGO-185 Trash Removal
    精度计算——减法
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4360170.html
Copyright © 2020-2023  润新知