HDU4289 Control

题目

Description

2000MS/65536K
You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
   all traffic of the terrorists must pass at least one city of the set.    sum of cost of controlling all cities in the set is minimal.

  You may assume that it is always possible to get from source of the terrorists to their destination.

1 Weapon of Mass Destruction

Input

There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).

Output

 For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.

Simple input

5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

Simple output

3

题目分析

其实这道题目也可以用最短路来做,但这里我们用LCA来做。用LCA基础再加上边权即可求出。
另外要注意,这个题需要扩栈操作否则会爆栈

AC代码

7996K/31MS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma comment(linker, "/STACK:10240000000,10240000000")///扩栈,要用c++交,用g++交并没有什么卵用。。。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define maxn 100005
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
struct nod
{
int v;///当前边序号
int dis;///边权
int next;
};
nod edge[maxn];
int dis[maxn],s[maxn],e[maxn];
///dis,s,e分别保存距离起点和终点;
int pre[maxn],lca[maxn],r[maxn];
///pre,lca,r分别代表父亲数组,e和s的LCA,先祖数组
int m,n,top;
bool vis[maxn];
int add_edge(int u,int v,int val)
{

//printf("1\n");
edge[top].v=u;
edge[top].dis=val;
edge[top].next=pre[v];
pre[v]=top++;
edge[top].v=v;
edge[top].dis=val;
edge[top].next=pre[u];
pre[u]=top++;
return 0;
}
int inil()
{

//memset(dis,0,sizeof(dis));
//memset(edge,-1,sizeof(edge));
memset(pre,-1,sizeof(pre));
memset(vis,false,sizeof(vis));
memset(r,0,sizeof(r));
top=0;
return 0;
}
int findset(int x)
{

//printf("1\n");
if(x!=r[x])
{
//printf("1\n");
r[x]=findset(r[x]); ///路径压缩
}
return r[x];
}
void tarjan(int k)
{

vis[k]=true;///标记k树已经被扫描过
r[k]=k;///让k自己的根节点为自己
for(int i=1;i<=m;i++)
{
//printf("1\n");
if(s[i]==k&&vis[e[i]])///如果s[i]与k节点相等且e[i]已经被扫描过
lca[i]=findset(e[i]);///则寻找e[i]的先祖节点
if(e[i]==k&&vis[s[i]])///反之则寻找s[i]的先祖节点
lca[i]=findset(s[i]);
}
for(int i=pre[k];i!=-1;i=edge[i].next)
{
//printf("1\n");
if(!vis[edge[i].v])
{
//printf("1\n");
dis[edge[i].v]=dis[k]+edge[i].dis;
tarjan(edge[i].v);
r[edge[i].v]=k;
}
}
}
int main()
{

int T;
scanf("%d",&T);
while(T--)
{
int a,b,c;
scanf("%d%d",&n,&m);
inil();
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
for(int i=1;i<=n;i++)
{
s[i]=0;
e[i]=0;
lca[i]=0;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
s[i]=a;
e[i]=b;
}
memset(vis,false,sizeof(vis));
dis[1]=0;
tarjan(1);
for(int i=1;i<=m;i++)
{
//printf("%d %d %d\n",dis[s[i]],dis[e[i]],2*dis[lca[i]]);
printf("%d\n",dis[s[i]]+dis[e[i]]-2*dis[lca[i]]);
}

}
}

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2586