HDU-1221 HDU Today 裸最短路 + STL

http://acm.hdu.edu.cn/showproblem.php?pid=2112

裸最短路 主要是练练map STL还是不太熟悉

 

这题起点可能等于终点 = = 坑了半个多小时

 

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<string>
#include<map>
using namespace std;
const int maxn = 205;
const int inf = 1<<28;
int n,m;
int maps[maxn][maxn],dis[maxn];
bool vis[maxn];

typedef pair<int,int>pii;
void Dijstra( int x )
{
    priority_queue<pii,vector<pii>,greater<pii> >que;
    memset( vis,0,sizeof(vis) );
    for( int i = 1; i <= n; i ++ )
        dis[i] = inf;
    dis[x] = 0;
    que.push( make_pair(dis[x],x) );
    while( !que.empty() )
    {
        pii u = que.top(); que.pop();
        int v = u.second;
        if( vis[v] )
            continue;
        vis[v] = true;
        for( int i = 1; i <= n; i ++ )
        {
            if( maps[v][i] != inf && dis[i] > dis[v] + maps[v][i] )
            {
                dis[i] = dis[v] + maps[v][i];
                que.push( make_pair( dis[i],i ) );
            }
        }
    }
}

int main()
{
    //freopen( "data.in","r",stdin );
    string s,e,s1,s2;
    int a,b,d;
    while( scanf("%d",&m) != EOF && m != -1 )
    {
        map<string,int>bus;
        for( int i = 0; i < maxn; i ++ )
        {
            for( int j = 0; j < maxn; j ++  )
            {
                if( i == j )
                    maps[i][j] = 0;
                else
                    maps[i][j] = inf;
            }
        }
        cin>>s>>e;

        bus[s] = 1; bus[e] = 2;
        int k = 3;
        for( int i = 1; i <= m; i ++ )
        {
            cin>>s1>>s2>>d;
            if( !bus[s1] )
            {
                bus[s1] = k++;
            }
            if( !bus[s2] )
            {
                bus[s2] = k++;
            }
            a = bus[s1];  b = bus[s2];
            if( d < maps[a][b] )
                maps[a][b] = maps[b][a] = d;
        }
        if( s == e )
        {
            puts("0");
            continue;
        }
        n = k-1;
        Dijstra(1);
        if( dis[2] == inf )
            puts("-1");
        else
            printf("%d\n",dis[2]);
    }
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值