The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the goods must be transported along the roads.
According the length of a road and the weight of the goods, there will be some cost during transporting. The cost rate of each road is the ratio of the cost to the weight of the goods transporting on the road. It is guaranteed that the cost rate is less than 1.
On the other hand, every city must wait till all the goods arrive, and then transport the arriving goods together with its own goods to the next city. One city can only transport the goods to one city.
Your task is find the maximum weight of goods which can arrive at the capital.
Input
There are several cases.
For each case, there are two integers N (2 <= N <= 100) and M in the first line, where N is the number of cities including the capital (the capital is marked by N, and the other cities are marked from 1 to N-1), and M is the number of roads.
Then N-1 lines follow. The i-th (1 <= i <= N - 1) line contains a positive integer (<= 5000) which represents the weight of goods which the i-th city will transport to the capital.
The following M lines represent M roads. There are three numbers A, B, and C in each line which represent that there is a road between city A and city B, and the cost rate of this road is C.
Process to the end of the file.
Output
For each case, output in one line the maximum weight which can be transported to the capital, accurate up to 2 demical places.
Sample Input
5 6
10
10
10
10
1 3 0
1 4 0
2 3 0
2 4 0
3 5 0
4 5 0
Sample Output
40.00
题意:
有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资。问给定N-1个城市将要提供的物资,和每条路的消耗百分比。求能送到首都的最多的物资数量
相当于求第n个城市到其余各个城市最“长”路径和(就是按着消耗百分比小的走)。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int mod=1000000007;
const int N=1e3+5;
const double pi=acos(-1);
const int inf=0x3f3f3f3f;
double mapp[110][110];
double dis[110];
int sign[110];
int val[110];
int t,n;
void dijkstra(int x)
{
for(int i=0; i<n;i++)
{
dis[i]=mapp[x][i];
sign[i]=0;
}
sign[x]=1;
for(int i=1; i<n; i++)
{
//0不一定是开始的节点
int k=-1;
double p=-1;
for(int j=0; j<n; j++)
{
if(sign[j]!=1&&p<dis[j])
{
k=j;
p=dis[j];//寻找百分比较大的
}
}
if(p==-1)
break;
sign[k]=1;
for(int j=0; j<n; j++)
{
if(sign[j]!=1&&dis[j]<p*mapp[k][j])
{
dis[j]=p*mapp[k][j];//更新百分比
}
}
}
double sum=0;
for(int i=1; i<n; i++)
{
sum+=val[i]*dis[i];
}
printf("%0.2lf\n",sum);
}
int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<n; i++)
{
scanf("%d",&val[i]);
}
for(int i=1; i<=n; i++)
{
//初始路径
for(int j=1;j<=n;j++)
{
if(i==j)
mapp[i][j]=1;
else
mapp[i][j]=0;
}
}
for(int i=0;i<m;i++)
{
//更新路径
int x,y;
double z;
scanf("%d%d%lf",&x,&y,&z);
if(mapp[x][y]<1.0-z)
{
mapp[x][y]=mapp[y][x]=1.0-z;
}
}
dijkstra(n);
}
return 0;
}