1784. Road Toll
Constraints
Time Limit: 1 secs, Memory Limit: 64 MB
Description
There are N cities, identified from 1 to N. There is one and only one road between every two cities. When somebody goes through a road, he must pay the toll. The toll is calculated like this: when he takes M money with him, he ought to pay M × r. The r may be different for different roads, and r is a real number between 0 and 1(inclusive). Now you want to go from city A to city B, how much money can be left? Suppose the money you took is 1 at the beginning.
Input
The first line contains an integer T, indicating the number of cases.
For each case, the first contains the three integer number N(3 ≤ N ≤ 100), A and B(1 ≤ A,B ≤ N). The following lines contain the N × N symmetric matrix, where each element shows the toll rate r from one city to another. You can suppose that all the elements of the matrix will be real numbers between 0 and 1(inclusive) and the diagonal of the matrix is always 0.
Output
For each case, output a real number rounded to 2 decimals, indicating the most money you can keep at last when you go from A to B.
Sample Input
1 3 1 3 0.00 0.10 0.20 0.10 0.00 0.10 0.30 0.20 0.00
Sample Output
0.81
#include <iostream> #include <vector> #include <memory.h> #include <cstring> #include <iomanip> #include <stdio.h> // for scanf and EOF using namespace std; double matrix[105][105]; vector<double> fee(105); vector<bool> visit(105,false); int n,a,b; void process() { //标记a附近的费用 //找到最大的点,标记为访问过,更新距离a的距离。 for (int i = 1; i <= n; ++i) { /* code */ visit[i]=0; fee[i]=1.00-matrix[a][i]; } int index=a; visit[a]=true; for (int i = 1;i<n; ++i) { /* code */ double max=0; int temp=0; for (int i = 1; i <= n; ++i) { /* code */ if(!visit[i]&&fee[i]>max) { temp=i; max=fee[i]; } } visit[temp]=true; if(temp==b) break; index=temp; for (int i = 1; i <= n; ++i) { /* code */ if(!visit[i] && (fee[index]-fee[index]*matrix[index][i]>fee[i])) fee[i]=fee[index]-fee[index]*matrix[index][i]; } } cout<<fixed<<setprecision(2)<<fee[b]<<endl; } int main() { int casen; cin>>casen; while(casen--) { cin>>n>>a>>b; memset(matrix,0,sizeof(matrix)); for (int i = 1; i <= n; ++i) { /* code */ for (int j = 1;j <= n; j++) { /* code */ cin>>matrix[i][j]; } } process(); } // system("pause"); }