HDU 2112 HDU Today Dijkstra

/*
----------------------------------------------------
    stratege : Dijkstra ;
    KeyPoint : Transform char to matrix ;
    status : 2012-05-17 00:06:30	Accepted	1003
                531 MS	328 KB	Visual C++	johnsondu
    URL :http://acm.hdu.edu.cn/showproblem.php?pid=2112
----------------------------------------------------
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std ;

const int MAXN = 155 ;
const int INF = 0xfffffff ;
struct Node
{
    char place[35] ;
} ;
Node Q[MAXN] ;
int mat[MAXN][MAXN] ;
int n, len;
int dist[MAXN] ;
int map[MAXN] ;
int m, res ;
bool flag ;

int max (int a, int b)
{
    return a > b ? a : b ;
}

int minr (int a, int b)
{
    return a > b ? b : a ;
}

void dijkstra ()
{
    int i, j;
    for (i = 1; i < m; i ++)
        dist[i] = mat[0][i] ;
    map[0] = true ;

    for (i = 1; i < m; i ++)
    {
        int min = INF, v ;
        for (j = 0; j < m; j ++)
        {
            if (!map[j] && min > dist[j])
                min = dist[j], v = j ;
        }
        if (min == INF)
            break ;
        map[v] = true ;
        for (j = 0; j < m; j ++)
        {
            if (!map[j] && dist[v] + mat[v][j] < dist[j])
                dist[j] = dist[v] + mat[v][j] ;
        }
    }
}

void input ()
{
    int i, j ;
    char tmp1[35], tmp2[35] ;
    bool flag1, flag2 ;
    int w, a, b ;
    for (i = 0; i < MAXN; i ++)
        {
            for (j = 0; j < MAXN; j ++)
                mat[i][j] = INF ;
            mat[i][i] = 1 ;
        }
        m = 0 ;
        flag = false ;
        scanf ("%s%s", Q[0].place, Q[1].place) ;
        memset (map, false, sizeof(map)) ;
        if (strcmp (Q[0].place, Q[1].place) == 0)
        {
            flag = true ;
        }
        len = 2 ;
        for (i = 0; i < n; i ++)   //用非常暴力的方法,将地点都赋一个下标,从0开始
        {
            scanf ("%s%s%d", tmp1, tmp2, &w) ;
            flag1 = false ;
            flag2 = false ;
            for (j = 0; j < len; j ++)
            {
                if (strcmp (Q[j].place, tmp1) == 0)  //如果地点已存在
                {
                    a = j ;
                    flag1 = true ;
                    break ;
                }
            }
            for (j = 0; j < len; j ++)
            {
                if (strcmp (Q[j].place, tmp2) == 0)  //如果地点已存在
                {
                    b = j ;
                    flag2 = true ;
                    break ;
                }
            }
            if (!flag1)  // 地点不存在
            {
                a = len ;
                strcpy (Q[len ++].place, tmp1) ;
            }
            if (!flag2)  // 地点不存在
            {
                b = len ;
                strcpy (Q[len ++].place, tmp2) ;
            }
            m = max (max (m, a), b);   //找到最大下标,以知道不同的地点个数
            mat[a][b] = mat[b][a] = w ; //转化为矩阵
        }
        m += 1 ;  // 注意节点个数
}

int main ()
{

    while (scanf ("%d", &n))
    {
        if (n == -1)
            break ;

        input () ;

        if (flag)       //起点终点相同
        {
            printf ("0\n") ;
            continue ;
        }

        dijkstra () ;

        if (dist[1] == INF)
            printf ("-1\n") ;
        else
            printf ("%d\n", dist[1]) ;
    }
    return 0 ;
}

你可能感兴趣的:(C++,struct,url,input,Matrix)