NOIP2009 Best Trade Point

Source: Internet
Author: User

Description
C state-owned N large cities and M-roads, each connecting one of the two cities in these n cities. There is a maximum of one road directly connected to any two cities. 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 size of the country, the distribution of resources everywhere is different, which leads to the price of the same commodity in various cities is 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, it decided to take advantage of the difference in the prices of the goods in different cities and make a little travel. 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 course of travel, any city can be repeated several times, but not required to pass through all n cities. Aaron earns travel through such trading methods: He chooses a passing city to buy his favorite product, the crystal ball, and sells the crystal ball in another city that passes by, using the difference as a travel fee. Since Aaron was mainly traveling to C, he decided that the trade would only take place at most once, and of course he would not have to trade without making a difference. Suppose the crystal ball price of the city of 1~n is 4,3,5,6,1, respectively. Aaron can choose one of the following lines: 1->2->3->5, who buys crystal ball at 3 of the price in city 2nd, sells crystal ball at 5 of the price in city 3rd and earns a travel number of 2. Aaron can also choose the following line 1->4->5->4->5, and in the 1th time to arrive in city 5th at 1 of the price of Crystal ball, the 2nd time to reach 4th City when the price of 6 sold crystal ball, the number of travel to earn 5.
now give the price of the crystal ball in n cities, the information about the M road (the number of the two cities connected to each road and the passage of the road). Please tell Aaron how much he can earn.

Input Format
The first line contains 2 positive integers n and m, separated by a space, representing the number of cities and the number of roads.
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, it means that the road is a one-way road between City X and City y, and if z=2, indicates that the road is a two-way road between City X and City Y.
output Format
The first line contains 2 positive integers n and m, separated by a space, representing the number of cities and the roadnumber.
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, it means that the road is a one-way road between City X and City y, and if z=2, indicates that the road is a two-way road between City X and City Y.
Test Sample 1
input
5 5
4 3 5) 6 1
1 2 1
1 4 1
2 3 2
3 5 1
4 5 2
Output
5

Notes
"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.

Analysis: This problem on the Internet is said to be on both sides of the SPFA, may be I write too clumsy it ... SPFA all timed out, no way, got 30 '.

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intn,m;intlink[500001],num[100001],next[500001];intlink2[500001],num2[100001], from[500001];inttot=0;intline[5000001];BOOLuse[500001];intmx[100001],mn[100001],p[100001];intRead () {CharCh=GetChar ();  while(ch<'0'|| Ch>'9') ch=GetChar (); intx=0;  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx;}voidInsertintXinty) {link[++tot]=y; NEXT[TOT]=NUM[X]; num[x]=tot; Link2[tot]=x; from[Tot]=num2[y]; num2[y]=tot;}voidspfa1 () {memset (MN,127,sizeof(MN)); intH=0, t=0; mn[1]=p[1]; use[1]=1; line[h]=1;  while(h<=t) {intx=Line[h]; intnow=Num[x];  while(now!=0)        {            intq=Link[now]; if(Mn[x]<mn[q] | | p[q]<mn[q]) mn[q]=min (mn[x],p[q]); if(!use[q]) line[++t]=Q; USE[Q]=1; now=Next[now]; } Use[x]=0; H++; }    return;}voidspfa2 () {memset (MX,-127,sizeof(MX)); intH=0, t=0; Mx[n]=p[n]; use[n]=1; line[h]=N;  while(h<=t) {intx=Line[h]; intnow=Num2[x];  while(now!=0)        {            intq=Link2[now]; if(Mx[x]>mx[q] | | p[q]>mx[q]) mx[q]=Max (mx[x],p[q]); if(!use[q]) line[++t]=Q; USE[Q]=1; now= from[now]; } Use[x]=0; H++; }    return;} intMain () {n=read (); m=read ();  for(intI=1; i<=n; i++) p[i]=read ();  for(intI=1; i<=m; i++)    {        intX=read (), Y=read (), z=read ();        Insert (x, y); if(z==2) Insert (Y,X); } spfa1 ();    SPFA2 (); intans=-1;  for(intI=1; i<=n; i++) {Ans=max (mx[i]-Mn[i],ans); } cout<< ans <<Endl; return 0;}

NOIP2009 Best Trade Point

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.