Original question: fzu 2169 http://acm.fzu.edu.cn/problem.php? PID = 1, 2169
This question seems to have two solutions: DFS and spfa. But how can DFS both be re? The spfa also uses an adjacent table to represent the edge. If it is expressed by a vector, it will use TLE, in addition, there is a difference or in spfa, that is, the topic is to walk along the shortest path to the capital city, but spfa is taking the shortest path to destroy the rebels and then go back to the capital city. I don't know what's going on, I don't know if I can explain it. In this case, if the number of rebels can destroy the number of rebels, do not use any algorithms. So I tried it and it turned into a big water question in an instant. I can no longer comment on this question, so I became a pastime.
Spfa: The spfa process is performed one by one from every military city. The only thing that can be done is that the rebels will destroy it.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <queue> # define mod 1000000007 using namespace STD; # define n 100007 struct edge {int V, next;} G [2 * n]; int head [N], TOT; int Army [N], rebel [N]; int vis [N], DIS [N]; int res; int n, m; void addedge (int u, int v) {G [tot]. V = V; G [tot]. next = head [u]; head [u] = tot ++;} void spfa (INT s) {int I; memset (VIS, 0, sizeof (VIS); que UE <int> que; while (! Que. Empty () que. Pop (); Que. Push (s); vis [s] = 1; DIS [s] = 0; while (! Que. Empty () {int TMP = que. Front (); Que. Pop (); vis [TMP] = 0; for (I = head [TMP]; I! =-1; I = G [I]. next) {int v = G [I]. v; If (DIS [v]> dis [TMP] + 1) {dis [v] = dis [TMP] + 1; if (! Vis [v]) {que. push (V); vis [v] = 1 ;}}} int main () {int I, j, X; int U, V; while (scanf ("% d", & N, & M )! = EOF) {memset (army, 0, sizeof (Army); memset (rebel, 0, sizeof (rebel); memset (Head,-1, sizeof (head); Tot = 0; int CNT = 0; for (I = 1; I <= N; I ++) {scanf ("% d ", & rebel [I]); If (rebel [I]) CNT ++;} for (I = 1; I <= m; I ++) scanf ("% d", & Army [I]); for (I = 0; I <n-1; I ++) {scanf ("% d ", & U, & V); addedge (u, v); addedge (v, U);} res = 0; for (I = 1; I <= m; I ++) {spfa (Army [I]); For (j = 1; j <= N; j ++) {If (DIS [J]! = Mod) {If (rebel [J]) {res + = rebel [J]; rebel [J] = 0; CNT -- ;}} if (CNT = 0) // break has been cleared;} printf ("% d \ n", Res);} return 0 ;}
View code
DFS method (runtime error ):
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <algorithm> # define mod 1000000007 # define ll long longusing namespace STD; # define n 100007 struct edge {int V, next;} G [2 * n]; int head [N], TOT; int Army [N], rebel [N]; ll sum; int n, m; void addedge (int u, int v) {G [tot]. V = V; G [tot]. next = head [u]; head [u] = tot ++;} int DFS (int u, int Val, int FA) {int Soni = 0; If (Ar My [u]) // find the army {sum + = val; Soni ++;} For (INT I = head [u]; I! =-1; I = G [I]. next) {int v = G [I]. v; If (V = FA) continue; Soni + = DFS (v, Val + rebel [u], U);} If (SONI> 1) sum-= (soni-1) * rebel [u]; return Soni;} int main () {int I, j, X; int U, V; while (scanf ("% d", & N, & M )! = EOF) {memset (army, 0, sizeof (Army); memset (rebel, 0, sizeof (rebel); memset (Head,-1, sizeof (head); Tot = 0; for (I = 1; I <= N; I ++) scanf ("% d", & rebel [I]); for (I = 1; I <= m; I ++) {scanf ("% d", & X); Army [x] = 1 ;} for (I = 0; I <n-1; I ++) {scanf ("% d", & U, & V); addedge (u, v ); addedge (v, U);} sum = 0; DFS (1, 0,-1); printf ("% LLD \ n", sum);} return 0 ;}
View code
Direct statistics:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;#define N 100007int army[N],rebel[N];int res;int n,m;int main(){ int i,j,x; int u,v; while(scanf("%d%d",&n,&m)!=EOF) { memset(rebel,0,sizeof(rebel)); int cnt = 0; res = 0; for(i=1;i<=n;i++) { scanf("%d",&rebel[i]); if(rebel[i]) res += rebel[i]; } for(i=1;i<=m;i++) scanf("%d",&army[i]); for(i=0;i<n-1;i++) scanf("%d%d",&u,&v); if(m == 0) { puts("0"); continue; } printf("%d\n",res); } return 0;}
View code