http://www.lydsy.com/JudgeOnline/problem.php?id=2435
How can I feel that water.
The pit is that DFS will explode ... All right.. With BFS.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int N=1000005;int S[n], CNT, ihead[n], q[n], vis[n], H[n],In[n], n;ll ans;struct dat {int Next, to, W;} e[n<<1];void Add (int u, int v, int w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;e[++cnt].next =IHEAD[V]; ihead[v]=cnt; E[cnt].to=u; E[cnt].w=w;} int BFS1 () {int front=0, Tail=0;for1 (i, 1, N) if (in[i]==1) q[tail++]=i;while (front!=tail) {int x=q[front++]; vis[x]=1; s[x ]++;RDM (x, i) if (!vis[e[i].to]) {int y=e[i].to;s[y]+=s[x];if (--in[y]==1) q[tail++]=y;}} return q[front-1];} void bfs2 (int x) {For1 (i, 1, N) vis[i]=0;int front=0, Tail=0;q[tail++]=x;while (front!=tail) {x=q[front++]; Vis[x]=1;int s S=h[x], SUM=S[X];RDM (x, i) if (!vis[e[i].to]) {int y=e[i].to;ans+=1ll*e[i].w*abs (ss+sum-s[y]-s[y]); h[y]=ss+sum-s[y]; q[tail++]=y;}}} int main () {read (n); For1 (i, 1, n-1) {int u=getint (), V=getint (), W=getint (), Add (U, V, W); in[u]++; in[v]++;} BFS2 (BFS1 ());p rintf ("%lld\n", ans); return 0;}
Description
There are n countries on planet W. For the economic development of their respective countries, they decided to
The construction of two-way roads makes connectivity between countries. But the kings of every country are so stingy that they only want to
To build exactly n–1 two-way road. The construction of each road costs a fee equal to the absolute value of the road length multiplied by the number of countries on both sides of the road. For example, in the dotted line, there are 2 or 4 countries at each end of the road, and if the road length is 1, the cost is 1x|2–4|=2. The number in the circle in the figure represents the country's number.
As the number of countries is very large, there are many ways to build roads, while the construction of each of these programmes
Cost is difficult to calculate manually, the king decided to find someone to design a software, for a given construction plan,
To figure out the costs that are required. Please help the Kings design a software like this.
Input
The first line of input contains an integer n, representing the number of countries on planet W, the country from 1 to n
Number. Next n–1 describes road construction, where line I contains three integers ai, bi, and CI, table
The two-way road is constructed between AI and bi two countries with a length of CI.
Output
Output An integer that represents the total cost to build all roads.
Sample Input
6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1
Sample Output
20
HINT
n = 1,000,000 1≤ai, bi≤n
0≤ci≤10^6
Source
Day2
"Bzoj" 2435: [Noi2011] Road construction (tree DP)