Travel Cost(dijkstra写法)

Travel Cost

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 11   Solved: 9
[ Submit][ Status][ Web Board]

Description

AC country is a famous and beautiful place. There are N cities in the country, numbered as 1,2,3...N。The first city is the capital of AC, so it is the greatest and best place all over the country. Many travelers often get a travel in AC.  ACMer and his girl friends like the fine view and beautiful scenery in AC, so they plan to get a travel in AC recently. AC and his N-1 girl friends go to the AC by plane  land at the capital airport. They wants to get a travel to every city of AC but independently. in other words, the first girl friend goes to the first city, the second girl friend goes to the second city,the rest can be done in the same manner.
In AC country, there maybe at most one road between every two different cities. One girl will spend some money if  she passes one road correspondingly. ACMer must offer all the travel cost of all his girl friends. He wants to known what is the minimum total cost if evry girl gets her travel by the most economic strategy ?

Input

There are many test case waiting for your program code!
In every case, the first line of the input will be N (1<=N<=100), the number of cites.
The rest of the input defines an N*N adjacency matrix. Each of its entries will be either an integer or the character x. The value of MAT(i,j) indicates the expense of sending a travel  directly from city i to city j. A value of x for MAT(i,j) indicates that there is no directly road from city to city j. 

Note that for a city to itself does not require any cost, so MAT(i,i) = 0 for all integer i in range(1,N). Also, you may assume that the road is undirected, so that MAT(i,j) = MAT(j,i). Thus the imput data only supply the entries on the lower triangular portion of adjacency matrix strictly. 

The input of your program will be the lower triangular section of matrix . That is, the second line of input will contain only, MAT(2,1). The next line will contain two entries, MAT(3,1) and MAT(3,2), and so on. All MAT(i,j) will not greater than 1000.

Output

for every case, output the minimum total cost of all ACMer's all girl friends,  one per line.

Sample Input

5
40
30 6
99 24 36
25 x x 25

Sample Output

141

通过俩种方法的比较发现在寻找单源最短路劲时候用Dijkstra还是比较快的,但是写法比较复杂,如果不超时的话还是用Floyd就可以,算法也比较友好



# include <iostream>
# include <string>
 
using namespace std;
 
int m[105][105];
const int maxn = 0x3f3f3f3f;
 
void fun()
{
    for(int i = 0; i < 105; i++)
    {
        for(int j = 0; j < 105; j++)
        {
            if(i == j)
            {
                m[i][j] = 0;
            }
            else
            {
                m[i][j] = maxn;
            }
        }
    }
}
 
int main(int argc, char *argv[])
{
    int n;
    while(cin >> n)
    {
        fun();
        for(int i = 2; i <= n; i++)
        {
            for(int j = 1; j < i; j++)
            {
                string s;
                cin >> s;
                if(s[0] != 'x')
                {
                    int v = 0;
                    for(int k = 0; k < s.size(); k++)
                    {
                        v = v * 10 + s[k] - '0';
                    }
                //    cout << "v = " <<v << endl;
                    m[i][j] = v;
                    m[j][i] = v;
                }
            }
        }
 
        int dis[105], book[105] = {0};
        //初始化dis数组从1到各个顶点的距离
        for(int i = 1; i <= n; i++)
        {
            dis[i] = m[1][i];
        }
        //表示第一个顶点用过
        book[1] = 1;
        //迪杰斯特拉核心算法
        for(int i = 1; i <= n - 1; i++)
        {
            //找到距离离1号顶点最近的顶点
            int minn = maxn;
            int flag;
            for(int j = 1; j <= n; j++)
            {
 
                if(!book[j] && dis[j] < minn)
                {
                    minn = dis[j];
                    flag = j;
                }
            }
            //这是一个确定值
            book[flag] = 1;
            //开始进行边的松弛
            for(int v = 1; v <= n; v++)
            {
                //判断是不是小于无穷值
                if(m[flag][v] < maxn)
                {
                    if(dis[v] > dis[flag] + m[flag][v])
                    {
                        dis[v] = dis[flag] + m[flag][v];
                    }
                }
            }
        }
/*
 
        cout << "----" << endl;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << m[i][j] << " ";
            }
            cout <<endl;
        }
        cout << "----" << endl;
*/
        int sum = 0;
        for(int i = 1; i <= n; i++)
        {
            sum += dis[i];
        }
        cout << sum << endl;
    }
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/i_o_fly/article/details/80073266