ZOJ 3794 Greedy Driver spfa

Source: Internet
Author: User

Question:

Given n points, m points have directed edges and mailbox capacity.

The starting point is 1, the ending point is n, and the mailbox is full of oil.

The m line below indicates the fuel consumption (that is, the length) of the start and end points and the edge)

The following is a digital m indicating that there are P gas stations, which can be filled with oil for free.

The P-digit line below represents the point mark of the gas station.

Next, an integer Q

The following Q-Row u v indicates that there is a sales station at the u point, and you can sell any number of oil in your mailbox, per unit v.

Ask how many yuan can be obtained at the end.


First, calculate the maximum remaining oil volume at each point f [I],

Then, reverse the edge to find the shortest path (dis) [I] from each point.

Then enumerate each sales point (f [I]-dis [I]) * v.


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; # define N 10050 # define M 200010 # define inf 1000000 # define ll long longstruct Edge {int from, to, dis, nex;} edge [M], e [M]; int head [N], edgenum; void add (int u, int v, int d) {Edge E = {u, v, d, head [u]}; edge [edgenum] = E; head [u] = edgenum ++;} int H [N], edg; void add2 (int u, int v, int d) {Edge E = {u, v, d, H [u]}; e [edg] = E; H [u] = edg ++;} int n, m, k; int F [N], T [N]; // F indicates the Station T indicates the point of sale int f [N]; // how much oil can be left from the start point to bool inq [N]; void spfa () {queue
       
         Q; memset (f,-1, sizeof f); memset (inq, 0, sizeof inq); f [1] = k; q. push (1); while (! Q. empty () {int u = q. front (); q. pop (); inq [u] = 0; for (int I = head [u]; ~ I; I = edge [I]. nex) {int v = edge [I]. to; if (f [v] <f [u]-edge [I]. dis) {if (F [v]) f [v] = k; elsef [v] = f [u]-edge [I]. dis; if (! Inq [v]) inq [v] = 1, q. push (v) ;}}} int dis [N]; // reverse the side and run the shortest short path from the end, that is, the minimum amount of void bfs () {for (int I = 1; I <= n; I ++) dis [I] = inf; memset (inq, 0, sizeof inq); dis [n] = 0; queue
        
          Q; q. push (n); while (! Q. empty () {int u = q. front (); q. pop (); inq [u] = 0; for (int I = H [u]; ~ I; I = e [I]. nex) {int v = e [I]. to; if (dis [v]> dis [u] + e [I]. dis) {if (F [v]) dis [v] = 0; elsedis [v] = dis [u] + e [I]. dis; if (! Inq [v]) inq [v] = 1, q. push (v) ;}}} void init () {memset (head,-1, sizeof head); edgenum = 0; memset (H,-1, sizeof H ); edg = 0;} int main () {int I, u, v, d; while (~ Scanf ("% d", & n, & m, & k) {init (); memset (F, 0, sizeof F); memset (T, 0, sizeof T); while (m --) {scanf ("% d", & u, & v, & d); add (u, v, d); add2 (v, u, d);} scanf ("% d", & m); while (m --) {scanf ("% d ", & u); F [u] = 1;} scanf ("% d", & m); while (m --) {scanf ("% d ", & u, & v); T [u] = v;} spfa (); if (f [n] =-1) {puts ("-1 "); continue;} bfs (); int ans = 0; for (int I = 1; I <= n; I ++) if (T [I] & f [I]! =-1 & dis [I]
         
          

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.