1784. Road Toll

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");
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值