【HDU 3409】Chase

Problem Description

Jack the Robber appears again! He just robbed a bank in town and is running away with a huge amount of dollar cash. Senior FBI agent Don Epps is taking responsibility to catch him. He must deploy his men immediately to hold up and capture Jack on some spot in town. Because this task is extremely urgent and crucial, Don asks his brother Charlie Epps, the mathematics professor in the University of CalSci, to help him find the theoretically best deployment.
Let us assume that the town can be described as N spots (numbered from 0 to N-1) and M bidirectional roads between those N spots. Jack the Robber starts running from Spot 0. After several rounds of chasing and fleeing in the past, Don summarized Jack’s rules of escape:
1.He will never visit a spot more than once. 
2.He is an expert about the geography of this town, so no matter which spot he reaches, the route he has traveled is always the shortest path from Spot 0 to that spot. To simplify the situation, we assume that there is only one shortest path from Spot 0 to any other spot. 
3.He has no certain destination spot to go. He just runs AS FAR AS HE CAN, following the two rules above. As you may see, if he never gets caught, he will definitely come to a spot where he has to stop because if he keeps running, he will violate the two rules above. When he reaches a spot where he has to stop and is not arrested by FBI at that spot, he hides and FBI will never find him.
4.When he comes to a spot and is NOT captured by FBI at that spot, he will randomly choose a road to continue running. Of course, his choice can’t conflict with the above mentioned rules. The term “randomly” means that he chooses each valid road on the same probability.
Even if Jack gets to a spot where agents are already deployed, he may still get a chance to run away. For example, if the spot is very crowded, even all agents are deployed there, Jack may still run away easily. So Charlie finds out an N×(P+1) probability matrix PT. PT(i, j) represents the probability of j FBI agents deployed at spot i arresting Jack when Jack comes to Spot i. For example, PT(0, 1) can be only 5%, which means sending only 1 agent to the initial spot may miss the target in large probability, because the agent may be killed by Jack. In another case, if Spot 5 is suitable for capturing and 10 agents are deployed there, PT(5, 10) may be over 80%. Of cause, PT(i, 0) is always 0.
According to the given matrix PT, you need to help Charlie to figure out the best way to deploy agents in which the probability of catching the robber reaches the maximum.

Input

Input contains several test cases. In the first line of each case, there are two integers N (N<=100) and M (M<=10000), representing the number of spots and the number of roads between them.
Next M lines describe all roads. In each line, there are three integers a, b and c (0<=a, b<N, 0<c<=10000), representing a road of length c between Spot a and Spot b. Pay attention that there may be more than one roads between two spots, as well as some roads may come into a self-loop (a=b). 
The following line contains an integer P (0<P<=50), representing the total number of agents. Following N lines describe the probability matrix PT. PT(i,j) (0<= PT(i,j)<=1) represents the probability that robber can be captured by j FBI agents in Spot i when he comes to Spot i . We don’t give the leftmost column of the matrix for they are all zero, so the matrix only has P columns. In other words, the first number you read from the matrix is PT(0,1) .
The input file ends with a line that contains “0 0”.

Output

For each test case, output a single line with a real number, representing the largest probability that the robber can be captured if all agents are deployed in the best way. The answer needs to be transferred into percentage pattern and rounded to 2 digits after decimal point.

Sample Input

4 4

0 1 1

0 2 2

1 3 3

2 3 1

2

0.01 0.1

0.5 0.8

0.5 0.8

0.7 0.9

0 0

Sample Output

60.00

题目大意:

  强盗从迷宫的0号点开始走,要求任一时刻走过的路径都是到达当前位置的最短路径,且有多条最短路径时选择字典序小的。如果有多种选择,则等概率选择任一种。现在一共有若干个警卫可以安放在这些节点上,并知道每个节点安放若干个警卫可以打败强盗的概率,求出如何安放可使打败强盗的概率最高。

分析:

  要求任一时刻走过的路径都是到达当前位置的最短路径,实际上就是建立一棵字典序最小的最小路径树,强盗从根节点走到叶子节点。然后在树上做树形概率DP就好了。

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3
  4 const int maxn = 5000;
  5 const int maxm = 50000;
  6 const int maxq = 100000;
  7
  8 int eto[maxm], enext[maxm], eweight[maxm];
  9 int efirst[maxn], en, sta;
 10 int tto[maxm], tnext[maxm], tfirst[maxn], tn;
 11 int que[maxq], h, r, dis[maxn];
 12 int n, m, u, v, w, p;
 13 bool in[maxn], vis[maxn];
 14 double win[2000][100], f[2000][100];
 15
 16 inline void ins (int fr, int to, int wi)
 17 {
 18     en++;
 19     enext[en] = efirst[fr];
 20     efirst[fr] = en;
 21     eweight[en] = wi;
 22     eto[en] = to;
 23 }
 24
 25 inline void ins2 (int fr, int to)
 26 {
 27     tn++;
 28     tnext[tn] = tfirst[fr];
 29     tfirst[fr] = tn;
 30     tto[tn] = to;
 31 }
 32
 33 void Bfs ()
 34 {
 35     memset (dis, 63, sizeof (dis));
 36     memset (que, 0, sizeof (que));
 37     memset (vis, 0, sizeof (vis));
 38     int node, edge, nto;
 39     que[h = r = 0] = sta;
 40     dis[sta] = 0;
 41     while (h <= r)
 42     {
 43         node = que[h];
 44         vis[node] = 0;
 45         for (edge = efirst[node]; edge; edge = enext[edge])
 46         {
 47             nto = eto[edge];
 48             if (dis[nto] > dis[node] + eweight[edge])
 49             {
 50                 dis[nto] = dis[node] + eweight[edge];
 51                 if (!vis[nto])
 52                     vis[que[++r] = nto] = 1;
 53             }
 54         }
 55         h++;
 56     }
 57 }
 58
 59 double max (double a, double b)
 60 {
 61     return a > b ? a : b;
 62 }
 63
 64 void Dfs (int node)
 65 {
 66     int size = 0;
 67     for (int i = tfirst[node]; i; i = tnext[i])
 68         size++;
 69     if (size == 0)
 70     {
 71         for (int j = 0; j <= p; j++)
 72             f[node][j] = win[node][j];
 73     }else
 74     {
 75         for (int i = tfirst[node]; i; i = tnext[i])
 76             Dfs (tto[i]);
 77         double temp[100];
 78         memset (temp, 0, sizeof (temp));
 79         for (int i = tfirst[node]; i; i = tnext[i])
 80         {
 81             for (int j = p; j >= 0; j--)
 82             {
 83                 for (int k = 0; k <= j; k++)
 84                 {
 85                     temp[j] = max (temp[j], f[tto[i]][k] / size + temp[j - k]);
 86                 }
 87             }
 88         }
 89         for (int j = p; j >= 0; j--)
 90         {
 91             for (int k = 0; k <= j; k++)
 92             {
 93                 f[node][j] = max (f[node][j], win[node][j - k] + (1 - win[node][j - k]) * temp[k]);
 94             }
 95         }
 96     }
 97 }
 98
 99 int main ()
100 {
101     while (1)
102     {
103         memset (eto, 0, sizeof (eto));
104         memset (eweight, 0, sizeof (eweight));
105         memset (enext, 0, sizeof (eweight));
106         memset (efirst, 0, sizeof (efirst));
107         memset (tto, 0, sizeof (tto));
108         memset (tnext, 0, sizeof (tnext));
109         memset (tfirst, 0, sizeof (tfirst));
110         memset (in, 0, sizeof (in));
111         memset (win, 0, sizeof (win));
112         memset (f, 0, sizeof (f));
113         scanf ("%d %d", &n, &m);
114         if (n == 0 && m == 0) return 0;
115         sta = en = tn = 0;
116         for (int i = 0; i < m; i++)
117         {
118             scanf ("%d %d %d", &u, &v, &w);
119             ins (u, v, w);
120             ins (v, u, w);
121         }
122         Bfs ();
123         for (int i = 0; i < n; i++)
124         {
125             for (int e = efirst[i]; e; e = enext[e])
126             {
127                 if (dis[eto[e]] == dis[i] + eweight[e])
128                 {
129                     if (in[eto[e]] == 0)
130                     {
131                         ins2 (i, eto[e]);
132                         in[eto[e]] = 1;
133                     }
134                 }
135             }
136         }
137         scanf ("%d", &p);
138         for (int i = 0; i < n; i++)
139             for (int j = 1; j <= p; j++)
140                 scanf ("%lf", &win[i][j]);
141         Dfs (0);
142         printf ("%.2lf\n", f[0][p] * 100);
143     }
144 }
时间: 2024-11-05 16:29:41

【HDU 3409】Chase的相关文章

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5811】Colosseo(拓扑+输入优化)

[HDU 5811]Colosseo(拓扑+输入优化) Colosseo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 446    Accepted Submission(s): 98 Problem Description Mr. Chopsticks keeps N monsters, numbered from 1 to N.

【HDU 5145】 NPY and girls(组合+莫队)

pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 593    Accepted Submission(s): 179 Problem Description NPY's girlfriend blew him out!H