现在的位置: 首页 > 综合 > 正文

ZOJ 1655 Transport Goods 最短路变形

2014年04月05日 ⁄ 综合 ⁄ 共 2396字 ⁄ 字号 评论关闭

点击打开链接

 

Transport Goods

 


 

Time Limit: 2 Seconds                                    
Memory Limit: 65536 KB                            

 


 

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个城市(包括首都,首都是N),每个城市都有一个运送的货物的重量,路与路之间有一个成本率。让你求最多有多少货物能够送到首都。

#include<stdio.h>
#include<string.h>
int val[107];
double dis[107],map[107][107];
int n,m;
double count=0;

void init()
{
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    map[i][j]=-1;
}

void dijstra()
{
    int vis[107];
    int i,j;
	double max;
	int flag=n;
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    vis[flag]=1;
    dis[flag]=1.0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        if(map[j][flag]!=-1&&!vis[j]&&dis[j]<dis[flag]*map[j][flag])
        {
            dis[j]=dis[flag]*map[j][flag];
        }
        max=0;
        for(j=1;j<=n;j++)
        if(!vis[j]&&dis[j]>max)
        {
            max=dis[j];
            flag=j;
        }
        vis[flag]=1;
    }
    for(i=1;i<n;i++)
    count+=dis[i]*val[i];
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        count=0;
        int i,j;
        init();
        for(i=1;i<n;i++)
        scanf("%d",&val[i]);
        int from,to;
        double rate;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%lf",&from,&to,&rate);
            if(map[from][to]<(1.0-rate))
            map[from][to]=map[to][from]=(1.0-rate);
        }
        dijstra();
        printf("%.2f\n",count);
    }
    return 0;
}

 

抱歉!评论已关闭.