luogu P1938找工就业

一头牛在一个城市最多只能赚D元,然后它必须到另一个城市工作。当然它可以在别处工作一阵子后,又回到原来的城市再最多赚D美元。而且这样的往返次数没有限制
城市间有P条单向路径,共有C座城市,编号1~C,奶牛当前处在城市S,路径i从城市Ai到Bi,在路径上行走不用任何花费

私人飞机服务。这条服务有F条单向航线,每条航线是从城市ji飞到另一个城市ki,费用是ti。若奶牛手中没有现钱,可以用后来赚的钱来付机票钱
奶牛可以选择在任何时候,在任何城市退休。若果在时间不做限制,奶牛总共可以赚多少钱?如果赚的钱也不会出现限制,就输出-1

输入格式:
第一行:5个用空格分开的整数D,P,C,F,S。
第2到第P+1行:第i+1行包含2个用空格分开的整数,表示一条从城市A_i到城市B_i的单向路径。
接下来F行,每行3个用空格分开的整数,表示一条从城市J_i到城市K_i的单向航线,费用是T_i。
输出格式:
一个整数,在上述规则下最多可以赚到的钱数。

最长路
加负边可,直接将松弛迭代的符号改成>也可
存在负环,处理一下负环,存在负环输出-1

(懒得再具体分析了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 500;
 4 struct enkidu {
 5     int y, nex, val;
 6 }e[maxn];
 7 int lin[maxn], len = 0;
 8 int f, c, d, p, s;
 9 int dis[maxn], du[maxn];
10 bool vis[maxn];
11 
12 inline int read() {
13     int x = 0, y = 1;
14     char ch = getchar();
15     while(!isdigit(ch)) {
16         if(ch == '-') y = -1;
17         ch = getchar();
18     }
19     while(isdigit(ch)) {
20         x = (x << 1) + (x << 3) + ch - '0';
21         ch = getchar();
22     }
23     return x * y;
24 }
25 
26 inline void insert(int x, int y, int v) {
27     e[++len].y = y;
28     e[len].val = v;
29     e[len].nex = lin[x];
30     lin[x] = len;
31 }
32 
33 queue<int> q;
34 inline void spfa(int st) {
35     memset(vis, 0, sizeof(vis));
36     memset(dis, 0xcfcf, sizeof(dis));
37     q.push(st);
38     vis[st] = 1;
39     dis[st] = d;
40     while(!q.empty()) {
41         int k = q.front(); q.pop();
42         vis[k] = 0;
43         for(int i = lin[k]; i; i = e[i].nex) {
44             int to = e[i].y, val = e[i].val;
45             if(dis[to] < dis[k] - val + d) {
46                    dis[to] = dis[k] - val + d;
47                    du[to]++;
48                    if(du[to] > c) {
49                        cout << -1 << '\n';
50                        exit(0);
51                 }
52                    if(!vis[to]) {
53                        vis[to] = 1;
54                        q.push(to);
55                 }
56             }
57         }
58     }
59 }
60 
61 int main() {
62     d = read(), p = read(), c = read(), f = read(), s = read();
63     for(int i = 1; i <= p; ++i) {
64         int x, y;
65         x = read(), y = read();
66         insert(x, y, 0);
67     }
68     for(int i = 1; i <= f; ++i) {
69         int x, y, v;
70         x = read(), y = read(), v = read();
71         insert(x, y, v);
72     }
73     spfa(s);
74     int ans = -1000;
75     for(int i = 1; i <= c; ++i)
76         ans = max(ans, dis[i]);
77     cout << ans << '\n';
78     return 0;
79 }

 

posted @ 2018-08-04 15:12  YuWenjue  阅读(141)  评论(0编辑  收藏  举报