HDU - 6201 transaction transaction transaction(spfa求最长路)

题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少。

分析:

1、从某个结点出发,首先需要在该结点a花费price[a]买书,然后再在边上行走,到达目的地后,在目的地b获得price[b]。

2、因此可以建立两个虚拟结点,

虚拟结点1连向n个点,边权分别为-price[i],表示以i为起点,需花费price[i]买书。

n个点连向虚拟结点2,边权分别为price[i],表示以i为终点,通过卖书可得price[i]。

3、spfa求最长路即可得最大利益。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const LL MOD = 998244353;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int n;
struct Edge{
    int v, cost;
    Edge(int vv = 0, int c = 0):v(vv), cost(c){}
};
vector<Edge> E[MAXN];
void addEdge(int u, int v, int w){
    E[u].push_back(Edge(v, w));
}
bool vis[MAXN];
int cnt[MAXN], dist[MAXN];
bool SPFA(int start){
    memset(vis, false, sizeof vis);
    memset(cnt, 0, sizeof cnt);
    for(int i = 0; i < MAXN; ++i) dist[i] = -INT_INF;
    queue<int> q;
    while(!q.empty()) q.pop();
    q.push(start);
    dist[start] = 0;
    vis[start] = true;
    cnt[start] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = 0; i < E[u].size(); ++i){
            int v = E[u][i].v;
            if(dist[v] < dist[u] + E[u][i].cost){
                dist[v] = dist[u] + E[u][i].cost;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                    if(++cnt[v] > n) return false;
                }
            }
        }
    }
    return true;
}
int price[MAXN];
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        for(int i = 0; i < MAXN; ++i) E[i].clear();
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i){
            scanf("%d", &price[i]);
        }
        int x, y, z;
        for(int i = 0; i < n - 1; ++i){
            scanf("%d%d%d", &x, &y, &z);
            addEdge(x, y, -z);
            addEdge(y, x, -z);
        }
        for(int i = 1; i <= n; ++i){
            addEdge(0, i, -price[i]);
            addEdge(i, n + 1, price[i]);
        }
        SPFA(0);
        printf("%d\n", dist[n + 1]);
    }
    return 0;
}

  

时间: 2024-10-29 19:09:40

HDU - 6201 transaction transaction transaction(spfa求最长路)的相关文章

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;amp;&amp;amp; SPFA求最长路 &amp;amp;&amp;amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accepted: 1383 Description It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous

HDU 2196 Computer(树形DP求最长路)

Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious a

(tarjan+SPFA求最长路) poj 3114

Countries in War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2803   Accepted: 843 Description In the year 2050, after different attempts of the UN to maintain peace in the world, the third world war broke out. The importance of indus

poj 3592 Instantaneous Transference 强连通图 缩点 再求最长路

1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 #define maxx 44 8 #define maxx2 44*44 9 #define INF 99999999 10 char s[maxx][maxx]; 11 bool tong[maxx2

【HDOJ1217】【Floyd求最长路】

http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9455    Accepted Submission(s): 4359 Problem Description Arbitrage is the use of discrepa

zoj-3795-Grouping-tarjan缩点求最长路

用tarjan进行缩点. 然后用dfs求最长路.水体... #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<stack> using namespace std; #define maxn 110000 vector<int>ol

zoj3795 Grouping --- 强连通,求最长路

给定图,求把至少把图拆成几个集合能够使集合内的点没有直接或间接关系. 首先由题意可得图中可能含环,而环里面的点肯定是要拆开的. 缩点建图得DAG图,可以想象一下..把图从入度为零的点向下展开,位于同一层的点放在一个集合是没有关系的, 那么题目所求的问题就转化成求图中最长路的问题了. 这个题的实质和 这题 其实是一模一样的.. #include <iostream> #include <cstring> #include <string> #include <cst

zoj 5303 Grouping 缩点求最长路

点击打开链接 Grouping Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-th message shows that the age of person si is not smaller than the age of person ti.