HDU4171--bfs+树

时间:2023-03-10 05:09:29
HDU4171--bfs+树

第一开始想成了DP。尼玛后来才发现只有N条边,那就简单了。。

从起点S遍历整棵树,从某点跳出来回到终点T,问最短路长度。然而从某点跳出时走过的路径是一个定值。。。。

长度为整棵树的边长和sum*2-d1[i]。。然后求这个值加上回学校的路长的最小值就好了

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#define INF 2000100000
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ; ll d2[maxn];
ll d1[maxn];
int vis[maxn]; struct Edge
{
int to, len;
} edge[maxn]; struct node
{
int id, d;
};
vector<Edge> g[maxn]; void bfs(int s)
{
memset(vis, , sizeof(vis));
memset(d1, , sizeof(d1));
d1[s] = ;
vis[s] = ;
queue<node> que;
que.push((node) {
,
});
while(!que.empty())
{
node x = que.front();
que.pop();
int p = x.id;
for(int i = ; i < g[p].size(); ++i)
{
Edge &e = g[p][i];
if(!vis[e.to]) {
d1[e.to] = d1[p] + e.len;
vis[e.to] = ;
que.push((node) {
e.to, d1[e.to]
});
}
}
}
}
int main()
{
//freopen("in", "r", stdin);
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; ++i) scanf("%I64d", d2 + i);
int from, to, len;
ll sum = ;
for(int i = ; i < n; ++i)
{
scanf("%d%d%d", &from, &to, &len);
g[from].push_back((Edge) {
to, len
});
g[to].push_back((Edge) {
from, len
});
sum += len;
}
sum *= ;
bfs();
//for(int i = 0; i <= n; ++i) printf("%d ",d1[i]);
//printf("\n");
ll mx = INF;
for(int i = ; i <= n; ++i)
{
mx = min(mx, sum - d1[i] + d2[i]);
}
cout << mx << endl;
for(int i = ; i <= n; ++i) g[i].clear();
}
}