P1073 optimal trade shortest 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 and output formats

Input formats:

 

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 formats:

 

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

 

A good question   

It can be layered shortest

A total of three

There are cost of each layer clearly to passage 0

The first layer is connected to the second layer represents a sell side -a] [x

Represents the second to third layers sell side connected to a [x]

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define LL long long
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 2147483647
const int N=1e6;
int dis[N],vis[N],head[N],n,m,s,pos,k,L,R,ans,a[N],x,y,z;
struct Edge
{
    int next,v,to;
}edge[N];
void add(int a,int b,int c)
{
    edge[++pos]=Edge{head[a],c,b};
    head[a]=pos;
}
void spfa(int s)
{
    queue<int>q;
    CLR(dis,-0x3f);

    q.push(s);
    dis[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].to;

            if(dis[v]<dis[u]+edge[i].v)
            {
                dis[v]=dis[u]+edge[i].v;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    RII(n,m);
    rep(i,1,n)RI(a[i]);
    rep(i,1,m)
    {
        RIII(x,y,z);
        add(x,y,0);add(x+n,y+n,0);add(x+2*n,y+2*n,0);
        add(x,y+n,-a[x]);add(x+n,y+2*n,a[x]);
        if(z==2)
        {
            swap(x,y);
            add(x,y,0);add(x+n,y+n,0);add(x+2*n,y+2*n,0);
            add(x,y+n,-a[x]);add(x+n,y+2*n,a[x]);
        }
    }
    spfa(1);
    cout<<max(dis[n],dis[3*n]);

    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/bxd123/p/10960520.html