Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1326747
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-17 21:44:57

Bright Network Hub

Time Limit:5sMemory limit:32M
Accepted Submit:61Total Submit:255

Microhard company has just invented a brandly new speeding up device called Bright Network Hub(BNH). The BNH can be installed on the fibers between two nodes. When a BNH works, all the information going through this fiber will cost half of the original time. If n BNHs are installed on the same fiber, the speed for going through this fiber will be 2^n times faster.

In order to improve the speed of campus network, students in FZU are planning to buy some BNHs. Since they don't have enough money, they can only afford to buy m BNHs. Given the network of n nodes, you are to install these m BNHs on the proper lines, so that the time needed from node 1 to node n can be minimized.

In the above network the minimal time needed from 1 to 5 is 44ms. Now if you have 2 BNHs. Installing both of them on the fiber 1-2, the time from 1 to 2 will become 8.5ms. However, the optimal scheme is to install them on 1-3 and 3-5.

Input

There are multiple test cases. The first line of each case contains two integers n and m (1<=n<=50, 1<=m<=10). The following n lines contains the network graph in adjcent matrix. There are n real numbers each line, representing the needed time in millisecond. If there is no fiber between two nodes, the corresponding entry will be 0. And it is guaranteed that there will always be a path from 1 to n.

Output

For each case, output only one line containing the minimal time from 1 to n, accurate to two fractional digits.

Sample Input

5 2
0 34 24 0 0
34 0 10 12 0
24 10 0 16 20
0 12 16 0 30
0 0 20 30 0

Sample Output

22.00

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int i, j, k, u;
    int fiber, bnh;
    double node[52][52][12];
    double path[52][12], min;
    int visit[52];
    int p;

    while (scanf("%d %d", &fiber, &bnh) != EOF)
    {
        for (i = 1; i <= fiber; ++i)
        {
            for (j = 1; j <= fiber; ++j)
            {
                scanf("%lf", &node[i][j][0]);
                for (k = 1; k <= bnh; ++k)
                    node[i][j][k] = node[i][j][k - 1] * 0.5;
            }
        }

        /* Dijkstra alg. */
        for (k = 0; k <= bnh; ++k)
        {
            memset(visit, 0, sizeof(visit));
            
            for (i = 1; i <= fiber; ++i)
                path[i][k] = node[1][i][k];
            visit[1] = 1;

            for (i = 1; i < fiber; ++i)
            {
                min = 1000000000;
                for (j = 1; j <= fiber; ++j)
                {
                    if ((visit[j] == 0) && (path[j][k] != 0) && (path[j][k] < min))
                    {
                        min = path[j][k];
                        p = j;
                    }
                }

                if (min == 1000000000)
                    break;

                visit[p] = 1;
                for (j = 1; j <= fiber; ++j)
                {
                    for (u = 0; u <= k; ++u)
                    {
                        if ((node[p][j][k - u] != 0) && ((path[j][k] == 0) || (path[j][k] > path[p][u] + node[p][j][k - u])))
                            path[j][k] = path[p][u] + node[p][j][k - u];
                    }
                }
            }
        }

        printf("%.2lf\n", path[fiber][bnh]);
    }
    
    return 0;
}

阅读(2176) | 评论(2) | 转发(0) |
0

上一篇:Count Common Ancestors

下一篇:Square(搜索)

给主人留下些什么吧!~~

micklongen2009-06-19 11:29:03

有些题目在浙大acm上可以找到,有些找不到。

chinaunix网友2009-06-19 01:31:23

浙大ACM上的题?