NC24858 [USACO 2009 Nov S]Job Hunt

题目链接

题目

题目描述

Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.
Bessie's world comprises P (1 <= P <= 150) one-way paths connecting C (2 <= C <= 220) cities conveniently numbered 1..C. Bessie is currently in city S (1 <= S <= C). Path i runs one-way from city Ai to city Bi (1 <= Ai <= C; 1 <= Bi <= C) and costs nothing to traverse.
To help Bessie, Farmer John will give her access to his private jet service. This service features F (1 <= F <= 350) routes, each of which is a one way flight from one city Ji to a another Ki (1 <= Ji <= C; 1 <= Ki <= C) and which costs Ti (1 <= Ti <= 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn't have the cash on hand.
Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full D dollars in each city she can travel to? Print -1 if there is no limit to this amount.

输入描述

  • Line 1: Five space-separated integers: D, P, C, F, and S
  • Lines 2..P+1: Line i+1 contains two space-separated integers that name a one-way path from one city to another: Ai and Bi
  • Lines P+2..P+F+1: Line P+i+1 contains three space-separated integers that name a one-way jet flight from one city to another and the price for that flight: Ji, Ki, and Ti

输出描述

  • Line 1: A single integer representing the most money she can make while following the law.

示例1

输入

100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120

输出

250

说明

This world has five cities, three paths and two jet routes. Bessie starts out in city 1, and she can only make 100 dollars in each city before moving on.
Bessie can travel from city 1 to city 5 to city 2 to city 3, and make a total of 4*100 - 150 = 250 dollars.

题解

知识点:最短路。

第一种路不需要任何花费,第二种路花费 \(T_i\) 。另外到达每个城市都能赚 \(D\) ,因此考虑每条边都加上 \(D\)。并且最后答案额外加一次 \(D\) ,因为起点城市也要算进去。

最后跑SPFA最长路,因为存在正权。

时间复杂度 \(O(k(P+F))\)

空间复杂度 \(O(C+P+F)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 307, M = 507;

int D, P, C, F, S;

template<class T>
struct Graph {
    struct edge {
        int v, nxt;
        T w;
    };
    int idx;
    vector<int> h;
    vector<edge> e;

    Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {}

    void clear(int n, int m) {//全局使用时清零,确定范围防止超时
        idx = 0;
        h.assign(n + 1, 0);
        e.assign(m + 1, { 0,0,0 });
    }

    void add(int u, int v, T w) {
        e[++idx] = edge{ v,h[u],w };
        h[u] = idx;
    }
};
Graph<int> g(N, M);

bool vis[N];
int dis[N], cnt[N];
queue<int> q;
bool SPFA(int st) {
    memset(dis, -0x3f, sizeof(dis));
    q.push(st);
    vis[st] = 1;
    dis[st] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = g.h[u];i;i = g.e[i].nxt) {
            int v = g.e[i].v, w = g.e[i].w;
            if (dis[v] < dis[u] + w) {
                dis[v] = dis[u] + w;
                cnt[v] = cnt[u] + 1;
                if (cnt[v] >= C) return false;
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return true;
}


int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> D >> P >> C >> F >> S;
    for (int i = 1;i <= P;i++) {
        int A, B;
        cin >> A >> B;
        g.add(A, B, D);
    }
    for (int i = 1;i <= F;i++) {
        int J, K, T;
        cin >> J >> K >> T;
        g.add(J, K, D - T);
    }
    int ans = -1;
    if (SPFA(S)) {
        for (int i = 1;i <= C;i++) ans = max(ans, dis[i]);
    }
    cout << ans + D << '\n';
    return 0;
}
posted @ 2023-01-03 19:33  空白菌  阅读(16)  评论(0编辑  收藏  举报