hdu 6201 深度優先搜索

Problem Description
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell. 
As we know, the price of this book was different in each city. It is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
 

Input
The first line contains an integer T (1T10) , the number of test cases. 
For each test case:
first line contains an integer n (2n100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1Price10000) 
then follows n1 lines, each contains three numbers xy and z which means there exists a road between x and y, the distance is zkm (1z1000)
 

Output
For each test case, output a single number in a line: the maximum money he can get.
 

Sample Input
1 4 10 40 15 30 1 2 30 1 3 2 3 4 10
 

Sample Output
8



真的,要不是當時評測機被那幫交腳本做題的傢伙搞炸了,這題肯定能在比賽時做出來。

注意:這題時間限制是2000ms!!!

說一下思路:

這題的意思就是:一個商人只能在一個城市內買書或買書,然後把所買的書賣到下一個城市,而且買和賣都只有一次,期間從一個城市到另一個城市的路徑長度要當成路費,問這個買賣的最大利潤是多少。

那麼這道題就是要你求一個點的點權,與另一個點的點權與兩點之間的權值(中間可能隔着幾個點)的和,的差的最大值。

即是:

maxx = max(maxx, points[maps[u][i].v] - points[st] - maps[u][i].w - dis);
maxx = max(maxx, points[st] - points[maps[u][i].v] - maps[u][i].w - dis);

這裏,maxx是指那個最大值,st 指 起點,maps[u][i].v 指 終點,u 指終點的前一個點,i 指 u 點連接點的個數,maps[u][i].w 指 u 到 v 的邊的權值,dis 指 起點到 u 點 的所有邊的權值和,points 數組就是記錄所有點的權值

maps在這裏是vector,其聲明爲:

struct node
{
    LL v, w;
};
vector<node>maps[maxn];

要是vector不會的話百度一下吧



最後附上代碼:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC萬歲!!!!!!!!!!!!!!
const int maxn = 100090;
const int inf = -999999999;
struct node
{
    int v, w;
};
vector<node>maps[maxn];
int points[maxn] = {}, maxx;
void dfs(int st, int u, int uv, int dis)
{
    for(int i = 0; i < maps[u].size(); i++)
    {
        if(maps[u][i].v == st || maps[u][i].v == uv) continue;
        maxx = max(maxx, points[maps[u][i].v] - points[st] - maps[u][i].w - dis);
        maxx = max(maxx, points[st] - points[maps[u][i].v] - maps[u][i].w - dis);
        dfs(st, maps[u][i].v, u, dis + maps[u][i].w);
    }
}
int main()
{
    int _;
    scanf("%d", &_);
    while(_--)
    {
        int n;
        int u, v, w;
        scanf("%d", &n);
        struct node nn;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &points[i]);
        }
        for(int i = 1; i < n; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            if(u > v)
            {
                swap(u, v);
            }
            nn.v = v;
            nn.w = w;
            maps[u].push_back(nn);
        }
        maxx = inf;
        for(int i = 1; i <= n; i++)
        {
            dfs(i, i, i, 0);
        }
        for(int i = 1; i <= n; i++)
        {
            maps[i].clear();
        }
        printf("%d\n", maxx);
    }
    return 0;
}
/*
4
30 40 10 40
1 3 20
1 2 40
1 4 10
*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章