• transaction transaction transaction 最大费用最大流转化到SPFA最长路


    //当时比赛的时候没有想到可以用SPFA做,TLE!

    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
     
    Recommend
     
     
    liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201 
     
     
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<functional>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    
    //最大费用最大流 简化到SPFA上特例
    const int MAXN = 100000 + 65;
    struct edge
    {
        int to, next, cost;
    }E[MAXN << 2];
    int head[MAXN], tot, n;
    int val[MAXN];
    bool vis[MAXN];
    int lowcost[MAXN];
    void init()
    {
        memset(head, -1, sizeof(head));
        tot = 0;
    }
    void addedge(int u, int v, int d)
    {
        E[tot].to = v;
        E[tot].cost = d;
        E[tot].next = head[u];
        head[u] = tot++;
    }
    int spfa(int st, int ed)
    {
        memset(vis, false, sizeof(vis));
        memset(lowcost, 0, sizeof(lowcost));
        queue<int> q;
        q.push(st);
        vis[st] = true;
        lowcost[st] = 0;
        while (!q.empty())
        {
            int f = q.front();
            q.pop();
            vis[f] = false;
            for (int i = head[f]; i != -1; i = E[i].next)
            {
                int v = E[i].to, d = E[i].cost;
                if (lowcost[v] < lowcost[f] + d)
                {
                    lowcost[v] = lowcost[f] + d;
                    if (!vis[v])
                    {
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        return lowcost[ed];
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while (T--)
        {
            init();
            scanf("%d", &n);
            for (int i = 1; i <= n; i++)
            {
                scanf("%d", &val[i]);
                addedge(0, i, val[i]);
                addedge(i, n + 1, -val[i]);
            }
            int u, v, d;
            for (int i = 0; i < n - 1; i++)
            {
                scanf("%d%d%d", &u, &v, &d);
                addedge(u, v, -d);
                addedge(v, u, -d);
            }
            printf("%d
    ", spfa(0, n + 1));
        }
    }
  • 相关阅读:
    Linux下权限的解释
    【小小帝国】部分攻略
    php上传大文件时php.ini的几处设置
    SSH登录常用工具推荐
    梅兰文化http://www.tzhl.gov.cn/col/col894/index.html
    PHP上传大文件 分割文件上传
    ssh连接Linux很慢,且ssh传输文件很慢的解决方案
    vi命令使用
    SWFUpload 2.5.0版 官方说明文档 中文翻译版
    利用Flash上传大文件,swfupload修改说明flash制作教程
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7515544.html
Copyright © 2020-2023  润新知