Optimal Trade (layered graph)

Title Description

C C owned the n- the n-th largest city and m m road, each road which connect  the n- the n-two cities in a city. Up the road is only one directly connected between any two cities. This  m road m bar there as part of the one-way road, as part of two-way traffic on the road, two-way traffic on the road at the statistics the number of counts as  1 1.

C C size of the country, the distribution of resources across the different, which leads to the same kind of goods is not necessarily the same price in different cities. However, the same product in the same city bid and the ask price is always the same.

Businessman Aaron came to the  C C country tour. When he learned that the same product may be different prices in different cities of this information, they decided to travel at the same time, the use of commodity earn a little travel in different cities in the post. Set  C C n cities in the country numbered from  1 ~ n 1 n, Aaron decided from  1 departure No. 1 city, and finally in  n n number of cities to end his trip. In the tourism process, any city can be repeated many times, but is not required to go through all  the n- the n-cities. Aaron earn travel through this trade: he would choose to go through a city to buy his favorite goods - crystal ball, and in another city after selling crystal ball, earned as a difference travel expenses. Aaron is mainly due to  C C country tour, he decided to carry on the trade only once at most, of course, make any difference in his case there is no need to trade. 

Assume that  C C state-owned  5 5 major cities, the number of cities and road connections in the following figure, the one-way arrows indicate this road is one-way, two-way arrow indicates this road is two-way traffic.

Suppose  1 ~ n 1 terrarium price n are numbers city  4,3,5,6,1 4 , 3 , 5 , 6 , 1. 

Aaron may be selected as a line: . 1 1-> 2 2-> 3 3-> 5 5, and  2 to 2 urban 3 3 price buying crystal ball, in  3 to No. 3 cities 5 selling price of 5 the ball, the number of travel 2 is earned.

Aaron may be selected as follows a line 1 1 -> . 4 4-> 5 5-> . 4 4-> 5 5 and the first 1 1 times reaches 5 to No. 5 city when  a price of buying a crystal ball, in the first  2 2 reach 4 to No. 4 when the city 6 travel of 6 selling price crystal ball and earn as 5 5

Now given  n crystal ball price, n cities m Information m of roads (each two cities are connected by roads and traffic numbers that section of the road). Please tell Aaron, he can earn a maximum of how much travel.

Input Format

The first row contains  2 two positive integers n- n-and  m number m, separated by a space, and represent the number of urban roads.

The second row of n a positive integer, each separated by a space between two integers, respectively, by the reference numeral order of the n cities commodity prices.

Subsequently  m m rows, each row having . 3 three positive integers X, Y, Z X , Y , Z, between two integers each separated by a space. If  Z. 1 = Z = . 1, which represents city road X X city y way road between y; if Z = 2 Z = 2, which represents urban road  X X city and y y is between the two-way road.

Output Format

An integer representing up to earn travel. If you do not trade, the output  0 0

Sample input and output

Input # 1
5 5 
4 3 5 6 1 
1 2 1 
1 4 1 
2 3 2 
3 5 1 
4 5 2 
Output # 1
5

Description / Tips

【data range】

Input data to ensure  a No. 1 city to reach the n- the n-number of cities.

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

For 30% of the data, 1≤n≤100 . 1 n- . 1 0 0.

For 50% of the data, a tourist route does not exist, you can start from one city, return to this city.

To 100% of the data, 1≤n≤100000 . 1 n- . 1 0 0 0 0 0, 1≤m≤500000 . 1 m . 5 0 0 0 0 0, 1≤x . 1 X, y≤n Y n-, 1≤z≤2 . 1 Z 2, 1 ≦ . 1 ≤ cities

Crystal Ball price ≤100 1 0 0.

 

 

See the problem, either dp, or bfs real hammer.

Analyze dp, in fact, very simple equation, the title has been simplified as much as possible the state (start reading the wrong questions feel so hard so hard).
So I simplified a bit topics:

Starting from 1, every city choose to buy or not to buy, if you buy, the next will have to sell, the answer to each contribute up to a point, seeking the maximum.

So the equation is very simple. (I do not want to push equation) can move freely in the state 1, state 2 can jump from the state 1, so I choose layered FIG. (The equation is very similar relaxation operation, but also clear the topic is the shortest (mistakenly) Which big brother big courageous can try dijkstra (manual funny))

FIG layered, each state is to build a layer of FIG., While the transfer is between the layers. In this question, we have three states:

1, not the hands of a crystal ball

2, buy a crystal ball

3, selling crystal ball

So, we built three map.

It is noted that: as to be able to move freely, so the right side of FIG between each layer is assigned 0

Finally ...... finally gone ah

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int n,m,head[maxn],cnt,dis[maxn],vis[maxn];
int a[maxn];
struct edge
{
int to,next,dis;
}e[maxn<<1];
inline void addedge(int from,int to,int dis)
{
e[++cnt].next=head[from];
e[cnt].to=to;
e[cnt].dis=dis;
head[from]=cnt;
}
queue < int > q;
void spfa(int s)
{
memset(dis,-0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
vis[s]=1;
dis[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(dis[v]<dis[u]+e[i].dis)
{
dis[v]=dis[u]+e[i].dis;
if(vis[v]==0)
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,0);
addedge(x+n,y+n,0);
addedge(x+2*n,y+2*n,0);
addedge(x,y+n,-a[x]);
addedge(x+n,y+2*n,a[x]);
if(z==2)
{
swap(x,y);
addedge(x,y,0);
addedge(x+n,y+n,0);
addedge(x+2*n,y+2*n,0);
addedge(x,y+n,-a[x]);
addedge(x+n,y+2*n,a[x]);
}
}
spfa(1);
printf("%d ",max(dis[n],dis[3*n]));
return 0;
}

 
 


Finally, (black drag the following two lines have a surprise for you)
bk201 AK IOI! ! !

@ bk201

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11291488.html