标签:number des 简化 str log init stream tween desc
//当时比赛的时候没有想到可以用SPFA做,TLE!
#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\n", spfa(0, n + 1)); } }
transaction transaction transaction 最大费用最大流转化到SPFA最长路
标签:number des 简化 str log init stream tween desc
原文地址:http://www.cnblogs.com/joeylee97/p/7515544.html