[NOIP2009] Improve group Rokua P1073 best Trade

Source: Internet
Author: User

Title Description

C state-owned N large cities and M-roads, each connecting one of the two cities in these n cities. Any two

There is at most one road directly connected to the city. Part of this M road is a one-way road, part

For two-way roads, two-way roads are counted as 1 in the number of statistical bars.

The vast territory of C countries, the distribution of resources everywhere, which led to the same commodity in different cities price

Not necessarily the same. However, the same product will always have the same bid and ask prices in the same city.

The merchant Aaron came to visit the C country. When he learned that the price of the same commodity in different cities might be different this information

After that, it was decided to take advantage of the difference between the goods in different cities to earn a little travel while traveling. Set C Country N Cities

The label of the city from 1~ N, Aaron decided to depart from City 1th, and eventually ended his travels in the City of N. In the tourism

In the process, any city can be repeated several times, but not required to pass through all n cities. Aaron through such a trade party

Type of travel: He will choose a passing city to buy his favorite goods-crystal ball, and after the other

A city sells this crystal ball and uses the difference earned as a travel fee. Since Aaron was mainly traveling to C country, he decided

This trade is only carried out at most once, of course, he does not have to trade when the difference is not earned.

Suppose C State 5 large cities, the number of cities and road connections such as, one-way arrows indicate this road

For one-way passes, the two-way arrow indicates that the road is bidirectional.

Suppose the crystal ball price of the city of 1~n is 4,3,5,6,1, respectively.

A-Aaron can choose one of the following lines: 1->2->3->5, and buy crystal ball at 3 of the price in city 2nd, at 3

The city sells crystal balls at 5 of the price and earns 2 of the travel costs.

Aaron can also choose one of the following lines 1->4->5->4->5, and at the 1th time to reach City 5th at 1 price

Buy crystal ball, sell crystal ball at 6 for the 2nd time to reach City 4th, earn 5 of the travel cost.

Now give the price of the crystal ball in n cities, information on M roads (the number of the two cities connected to each road)

And the prevailing conditions of the road). Please tell Aaron how much he can earn.

Input/output format

Input format:

The first line contains 2 positive integers n and m, separated by a space, representing the number of cities and the road

Number.

The second row n positive integers, separated by a space between each of the two integers, represent the N cities in the order of the labels.

Commodity prices of the city.

Next m lines have 3 positive integers per line, x, Y, Z, and each two integers separated by a space. If Z=1,

Indicates that the road is a one-way road between City X and City y; if z=2, it means the road for City X and City

Two-way road between Y.

Output format:

Output file Trade.out A total of 1 lines, containing 1 integers, representing the maximum amount of travel to be earned. If trade is not carried out,

The output is 0.

Input and Output Sample input example # #:
Sample # # of output:
5
Description

"Data Range"

Enter data to ensure that City No. 1th can reach City N.

For 10% of data, 1≤n≤6.

For 30% of data, 1≤n≤100.

For 50% of the data, there is no tourist route, you can start from a city, and then back to the city.

For 100% of data, 1≤n≤100000,1≤m≤500000,1≤x,y≤n,1≤z≤2,1≤ cities

Crystal ball price ≤100.

NOIP 2009 Raising the third question of the group

is to the BFS contract Unicom, reverse BFS award the lowest price on the path.

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8#include <queue>9 using namespacestd;Ten Const intmxn=120000; One intRead () { A     intx=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} the     returnx*F; - } -vector<int>E1[MXN],EF[MXN]; - intW[MXN],MINI[MXN]; + intn,m; - BOOLARR[MXN]; +queue<int>Q; A voidBFS () { at Q.push (n); -arr[n]=1; -      while(!Q.empty ()) { -         intu=Q.front (); Q.pop (); -          for(intI=0; I<ef[u].size (); i++){ -             intv=Ef[u][i]; in             if(!Arr[v]) { -arr[v]=1; to Q.push (v); +             } -         } the     } *     return; $ }Panax Notoginseng BOOLINQ[MXN]; - voidSPFA () { theQ.push (1); +inq[1]=1; A      while(!Q.empty ()) { the         intU=q.front (); Q.pop (); inq[u]=0; +          for(intI=0; I<e1[u].size (); i++){ -             intv=E1[u][i]; $             inttmp=min (mini[v],min (mini[u],w[u)); $             if(mini[v]>tmp) { -mini[v]=tmp; -                 if(!Inq[v]) { theinq[v]=1; - Q.push (v);Wuyi                 } the             } -         } Wu     } -     return; About } $ intMain () { -     inti,j; -     intx, y, z -N=read (); m=read (); Amemset (Mini,0x3f,sizeofmini); + //for (I=1;i<=n;++i) W[i]=read (), mini[i]=w[i]; the      for(i=1; i<=n;++i) w[i]=read (); -      for(i=1; i<=m;++i) { $X=read (); Y=read (); z=read (); the         if(z==1){ the e1[x].push_back (y); the ef[y].push_back (x); the         } -         Else{ in e1[x].push_back (y); the e1[y].push_back (x); the ef[x].push_back (y); About ef[y].push_back (x); the         } the     } the SPFA (); +      while(!q.empty ()) Q.pop (); - BFS (); the     intans=0;Bayi      for(i=1; i<=n;i++){ the         if(Arr[i]) Ans=max (ans,w[i]-mini[i]); the     } -printf"%d\n", ans); -     return 0; the}

[NOIP2009] Improve group Rokua P1073 best Trade

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.