Minimum P1576 cost and P1576 cost
Question background description
Some of the n people can transfer funds between their bank accounts. The fees for transfers between these individuals are different. Given the need to deduct A few percent of the service charge from the transfer amount when transferring funds between these people, may I ask how much A requires at least so that B will receive 100 yuan after the transfer.
Input/Output Format
Input Format:
Enter two positive integers n and m in the first line, indicating the total number of people and the number of people who can transfer money to each other.
Input three positive integers x, y, and z in each row in the following m rows, indicating that the person with the x mark and the person with the y mark must deduct the fee of z % (z <100) for mutual transfer ).
Enter two integers A and B in the last line. Data ensures direct or indirect transfer between A and B.
Output Format:
Output A to make B receive the total cost of at least 100 yuan. Accurate to the last 8 digits of the decimal point.
Input and Output sample input sample #1:
3 3 1 2 12 3 21 3 31 3
Output sample #1:
103.07153164
Description
1 <= n <= 2000
Train of Thought: At first, I made a Dijkstra record path, and then I tried to find out what I had to say.
The correct method is to obtain the weight of each edge and then directly launch the brute force attack.
Note: Because Division exists, it is greater than the number smaller than the number, which may be opposite to the ordinary Dijkstra.
0 Points of code
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace std; 5 int map [3000] [3000]; 6 double money = 100; 7 int dis [3000]; 8 int maxn = 0x7fffff; 9 int pass [3000]; 10 int vis [3000]; 11 int n, m; 12 int ans [1001]; 13 void print (int bg, int ed) 14 {15 16 int now = 1; 17 ans [now] = ed; 18 now ++; 19 int tmp = pass [bg]; 20 while (tmp! = Ed & tmp! = 0) 21 {22 ans [now] = tmp; 23 now ++; 24 tmp = pass [tmp]; 25} 26 ans [now] = bg; 27 int qq = ed; 28 for (int I = 2; I <= now; I ++) 29 {30 money = money/(double) (100-map [ans [I-1] [ans [I])/100); 31} 32 printf ("%. 8lf ", money); 33} 34 void Dijkstra (int p) 35 {36 memset (dis, 0x7f, sizeof (dis); 37 vis [p] = 1; 38 for (int I = 1; I <= n; I ++) 39 {40 dis [I] = map [p] [I]; 41} 42 for (int I = 1; I <= n; I ++) 43 {44 int minn = maxn; 45 int k; 46 for (int j = 1; j <= n; j ++) 47 {48 if (vis [j] = 0 & dis [j] <minn) 49 {50 minn = dis [j]; 51 k = j; 52} 53} 54 vis [k] = 1; 55 for (int j = 1; j <= n; j ++) 56 {57 if (dis [j]> dis [k] + map [k] [j]) 58 {59 dis [j] = dis [k] + map [k] [j]; 60 pass [j] = k; 61} 62} 63} 64} 65 int main () 66 {67 memset (map, 0x7f, sizeof (map); 68 scanf ("% d ", & n, & m); 69 for (int I = 1; I <= n; I ++) 70 {71 int x, y, z; 72 scanf ("% d", & x, & y, & z); 73 map [x] [y] = z; 74 map [y] [x] = z; 75} 76 int a, B; 77 scanf ("% d", & a, & B ); 78 Dijkstra (B); 79 print (B, a); 80 return 0; 81}View Code
AC code
1 # include <iostream> 2 # define MAXN 2001 3 # define inf 99999 4 using namespace std; 5 int N, M, A, B; 6 double m [MAXN] [MAXN]; 7 int main () 8 {9 int I, j; 10 cin> N> M; 11 cout. setf (ios: fixed); 12 for (I = 0; I <N; I ++) 13 for (j = 0; j <N; j ++) 14 m [I] [j] = 1/inf; 15 for (I = 0; I <M; I ++) 16 {17 int x, y, t; 18 cin> x> y> t; 19 x --; y --; 20 m [x] [y] = m [y] [x] = 1-(t/100.0); // exchange to weight21} 22 cin> A> B; 23 A --; B --; 24 // dijks Tra25 double dis [MAXN]; // dis I: money needed to trans 100 to i26 bool book [MAXN]; 27 book [B] = true; 28 for (I = 0; I <N; I ++) 29 {30 dis [I] = 100/m [B] [I]; // init dis [] 31 book [I] = false; // init book [] 32} 33 for (j = 0; j <N; j ++) 34 {35 int nmin; 36 double min = inf; 37 for (I = 0; I <N; I ++) 38 if (dis [I] <min &&! Book [I]) 39 {40 nmin = I; 41 min = dis [nmin]; // find # min-> nmin42} 43 book [nmin] = true; // record in book [] 44 for (I = 0; I <N; I ++) 45 if (min/m [nmin] [I] <dis [I] &! Book [I]) // relax46 dis [I] = min/m [nmin] [I]; 47} 48 cout. precision (8); 49 cout <dis [A]; 50 return 0; 51}View Code