首页 > 其他分享 >NC24858 [USACO 2009 Nov S]Job Hunt

NC24858 [USACO 2009 Nov S]Job Hunt

时间:2023-01-03 19:46:31浏览次数:63  
标签:NC24858 city int make Hunt USACO vis Bessie dis

题目链接

题目

题目描述

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;
}

标签:NC24858,city,int,make,Hunt,USACO,vis,Bessie,dis
From: https://www.cnblogs.com/BlankYang/p/17023194.html

相关文章

  • P1217 [USACO1.5]回文质数 Prime Palindromes
    题目题目描述因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以151是回文质数。写一个程序来找出范围[a,b](5<=a<b<=100,000,000)(一亿)间的......
  • SSDT hook——todo,待实践,并用pchunter
    【技术分享】r0下的进程保护安全客发布于2022-01-1717:00:36阅读1852https://www.wangan.com/p/7fy7fg9985331389 前言进程保护是众多AV或者病毒......
  • USACO 2019 January Contest, Silver
    2019JanuarySilverT1.GrassPlanting这道题是一道结论题:我们发现点的度数越多,答案越多,所以我们可以发现答案为最大的点的度数+1。#include<bits/stdc++.h>usingnam......
  • AcWing1170. 排队布局[USACO05]
    解题思路\(\qquad\)这题也是一个比较裸的差分约束:多了的那个输出\(-2\)的其实就是在差分约束系统中\(1\)号点和\(n\)号点没有约束关系,也就是\(1\)和\(n\)号不连通。由于这......
  • P2894 [USACO08FEB]Hotel G
    \(P2894\)[\(USACO08FEB\)]\(Hotel\)\(G\)一、题目描述参考样例,第一行输入\(n,m\),\(n\)代表有\(n\)个房间,编号为\(1-\simn\),开始都为空房,\(m\)表示以下有\(m\)行操作......
  • [USACO22DEC] Cow College B 题解
    洛谷P8897AcWing4821题目描述有\(n\)头奶牛,每头奶牛愿意交的最大学费位\(c_i\),问如何设置学费,可以使赚到的钱最多。\(1\len\le10^5,1\lec_i\le10^6\)做法分析......
  • [USACO22FEB] Sleeping in Class B
    好题分享——[USACO22FEB]SleepinginClassB洛谷题目链接题面翻译\(T\)组数据,每组给定一个长度为\(N\)的数组\(a_1,a_2,\dotsb,a_n\)。每次操作可选择两个......
  • P3128 [USACO15DEC]Max Flow P 树上点差分
    //题意:给出一棵树,现在有一操作:给出两点,将两点之间的路径都加1,最后统计整棵树中值最大的点是谁//思路:树上路径问题,树剖+线段树可以解决,但是因为只是简单的维护区间加减,用......
  • P2895 [USACO08FEB]Meteor Shower S
    P2895[USACO08FEB]MeteorShowerS#include<iostream>#include<cstring>#include<queue>usingnamespacestd;constintN=310;intx,y,t;intg[N][N];......
  • [USACO2
    [USACO2.4]两只塔姆沃斯牛TheTamworthTwo题目描述两只牛逃跑到了森林里。FarmerJohn开始用他的专家技术追捕这两头牛。你的任务是模拟他们的行为(牛和John)。追击在......