TOJ 2967: Job Hunt -- Bellman_Ford

2967: Job Hunt

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 9            Accepted:7

Description

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 ≤ SC). Path i runs one-way from city Ai to city Bi (1 ≤ Ai C; 1 ≤ BiC) 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 ≤ JiC; 1 ≤ KiC) 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.

Input

* 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

Output

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

Sample Input

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

Sample Output

250

Source

USACO Nov 2009


分析:带负权回路的单源最短路。

#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
         
         
#include 
         
         
           #include 
           #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #define mp make_pair using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef pair 
                     
                       pii; typedef vector 
                      
                        vi; typedef vi::iterator vi_it; typedef map 
                       
                         mii; typedef priority_queue 
                        
                          pqi; typedef priority_queue 
                         
                           , greater 
                          
                            > rpqi; const int MAX_C = 220 + 2; const int MAX_P = 500 + 2; const int INF = 500000000; int dp[MAX_C]; int cnt = 0; struct edge { int u; int v; int w; } e[MAX_P]; class Bellman_Ford { public: Bellman_Ford(int _n, int _s); bool bellman_ford(); private: int n, s; void initialize_single_source(); inline void relax(int u, int v, int duv); }; Bellman_Ford::Bellman_Ford(int _n, int _s) : n(_n), s(_s) { } void Bellman_Ford::initialize_single_source() { for (int i = 1; i <= n; ++i) { dp[i] = -INF; } dp[s] = 0; } inline void Bellman_Ford::relax(int u, int v, int duv) { if (dp[v] < dp[u] + duv) { dp[v] = dp[u] + duv; } } bool Bellman_Ford::bellman_ford() { initialize_single_source(); for (int k = 1; k < n; ++k) { for (int i = 0; i < cnt; ++i) { relax(e[i].u, e[i].v, e[i].w); } } for (int i = 1; i < cnt; ++i) { if (dp[e[i].v] < dp[e[i].u] + e[i].w) { return false; } } return true; } int main(int argc, char *argv[]) { // freopen("D:\\in.txt", "r", stdin); int d, p, c, f, s; cin >> d >> p >> c >> f >> s; while (p--) { int a, b; cin >> a >> b; e[cnt].u = a; e[cnt].v = b; e[cnt++].w = d; } while (f--) { int j, k, t; cin >> j >> k >> t; e[cnt].u = j; e[cnt].v = k; e[cnt++].w = d - t; } Bellman_Ford bf(c, s); if (bf.bellman_ford()) { int maxv = 0; for (int k = 1; k <= c; ++k) { maxv = max(maxv, dp[k]); } cout << maxv + d << endl; } else { cout << -1 << endl; } return 0; } 
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
         
        
        
       
       
      
      
     
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值