TOJ 3018 ZOJ 1655 Transport Goods / dijkstra

Transport Goods

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
总提交: 52            测试通过:13

描述

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.

输入

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.

输出

For each case, output in one line the maximum weight which can be transported to the capital, accurate up to 2 demical places.

样例输入

5 6
10
10
10
10
1 3 0
1 4 0
2 3 0
2 4 0
3 5 0
4 5 0

样例输出

40.00

最短路变形 存的是最大的比例吧

#include <string.h>
#include <math.h>
#include <stdio.h>
#define inf 0x7fffffff
const int MAX = 110;
double a[MAX][MAX];
double dis[MAX];
int vis[MAX];
int mm[MAX];
int n;
	
void init()
{
	int i,j;
	for(i = 0; i <= n; i++)
		for(j = 0;j <= n; j++)
			a[i][j] = 0;
}
void dijkstra()
{
	int i,j,k;
	double min;
	memset(vis,0,sizeof(vis));
	for(i = 1; i <= n; i++)
		dis[i] = a[n][i];
	dis[n] = 1;
	for(i = 1;i < n; i++)
	{
		min = -1.0;
		for(j = 1; j <= n; j++)
		{
			if(min < dis[j] && !vis[j])
			{
				min = dis[j];
				k = j;
			}
		}
		if(min == -1.0)
			break;
		vis[k] = 1;
		for(j = 1; j <= n; j++)
		{
			if(!vis[j] && dis[j] < a[k][j] * min)
				dis[j] = a[k][j] * min;
		}
	}
}
int main()
{
	int i,j,m,x,y;
	double z,sum;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		init();
		for(i = 1;i <= n - 1; i++)
			scanf("%d",&mm[i]);
		while(m--)
		{
			scanf("%d %d %lf",&x,&y,&z);
			if(a[x][y] < 1.0 - z)
			{
				a[x][y] = 1.0 - z;
				a[y][x] = 1.0 - z;
			}
		}
		sum = 0;
		dijkstra();
		for(i = 1; i < n; i++)
			sum += mm[i] * dis[i];
		printf("%.2f\n",sum);
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值