zoukankan      html  css  js  c++  java
  • HDU6201

    transaction transaction transaction

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
    Total Submission(s): 706    Accepted Submission(s): 357

    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
     

    Source

     

    建立源点和汇点。

    源点连所有的树上点, 边权为 -a[i],表示起点,需要花费a[i],所有树上点在连接 汇点, 边权为a[i],表示收益为a[i],然后在根据树建图,边权为-w,表示花费w。 

    然后spfa跑个最长路,答案为dis[汇点]。

     1 //2017-09-11
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <queue>
     7 
     8 using namespace std;
     9 
    10 const int N = 110000;
    11 const int INF = 0x3f3f3f3f;
    12 int head[N], tot;
    13 struct Edge{
    14     int v, w, next;
    15 }edge[N<<2];
    16 
    17 void init(){
    18     tot = 0;
    19     memset(head, -1, sizeof(head));
    20 }
    21 
    22 void add_edge(int u, int v, int w){
    23     edge[tot].v = v;
    24     edge[tot].w = w;
    25     edge[tot].next = head[u];
    26     head[u] = tot++;
    27 }
    28 
    29 bool vis[N];
    30 int dis[N];
    31 int cnt[N];
    32 deque<int> dq;
    33 bool spfa(int s, int n){
    34     memset(vis, 0, sizeof(vis));
    35     memset(cnt, 0, sizeof(cnt));
    36     for(int i = 0; i <= n+1; i++)
    37           dis[i] = -INF;
    38     vis[s] = 1;
    39     dis[s] = 0;
    40     cnt[s] = 1;
    41     deque<int> dq;
    42     dq.push_back(s);
    43     while(!dq.empty()){
    44         int u = dq.front();
    45         dq.pop_front();
    46         vis[u] = 0;
    47         for(int i = head[u]; i != -1; i = edge[i].next){
    48             int v = edge[i].v;
    49             if(dis[v] < dis[u] + edge[i].w){
    50                 dis[v] = dis[u] + edge[i].w;
    51                 if(!vis[v]){
    52                     vis[v] = 1;
    53                     dq.push_back(v);
    54                     if(++cnt[v] > n)return false;
    55                 }
    56             }
    57         }
    58     }
    59     return true;
    60 }
    61 
    62 int arr[N], n;
    63 
    64 int main()
    65 {
    66     int T;
    67     scanf("%d", &T);
    68     while(T--){
    69         scanf("%d", &n);
    70         init();
    71         int s = 0, t = n+1;
    72         for(int i = 1; i <= n; i++){
    73             scanf("%d", &arr[i]);
    74             add_edge(s, i, -arr[i]);
    75             add_edge(i, t, arr[i]);
    76         }
    77         int u, v, w;
    78         for(int i = 0; i < n-1; i++){
    79             scanf("%d%d%d", &u, &v, &w);
    80             add_edge(u, v, -w);
    81             add_edge(v, u, -w);
    82         }
    83         spfa(s, n);
    84         printf("%d
    ", dis[t]);
    85     }
    86 
    87     return 0;
    88 }
  • 相关阅读:
    网络运维与管理2013超值精华本
    [置顶] JQuery实战总结三 标签页效果图实现
    ASP.NET 联想控件(Autocomplete)测试可用 ascx
    python手记(48)
    [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换
    「两」创建一个带 ssh 镜座服务(修订版)--采用 Dockerfile 创
    美国同事实习
    javascript相框echarts插件实现酷立方效果图的人
    Docker container 集装箱说明
    tinkerpop(1) 地图数据库console科研
  • 原文地址:https://www.cnblogs.com/Penn000/p/7505936.html
Copyright © 2011-2022 走看看