hdu 2112 HDU Today(图论:迪杰斯特拉)

一道模板题,WA了30+次

原因是题目没说清楚

1、这个图是双向图

2、可能会出现从一个点a到点a这种情况,应该输出0

3、可能起点或终点不再给出的站牌中出现,这是输出-1

还有一点原因是CE了10+次

因为用了map的find和count函数判别字符串是否出现

在本机上编译不会出错,但是交题就会各种Compile Error

大概是因为oj支持的c++版本比较低吧

百度后发现一个解决方法是添加#include <string>头文件

1609ms代码如下:

#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 210
#define LL long long
#define INF 0x7fffffff
using namespace std;

int g[MAXN][MAXN];
bool vis[MAXN];
int d[MAXN];
string st, en, s, e;
map<string, int> m;
map<string, int>::const_iterator it;

void Dijstra(int s, int cnt) {
    int i, j, x, tmp;
    memset(vis, 0, sizeof(vis));
    for(i=0; i<cnt; ++i)
        d[i] = INF;
    d[s] = 0;
    for(i=0; i<cnt; ++i) {
        tmp = INF;
        for(j=0; j<cnt; ++j) {
            if(!vis[j] && d[j]<tmp)
                tmp = d[x = j];
        }
        vis[x] = true;
        for(j=0; j<cnt; ++j) {
            if(g[x][j] < INF)
                d[j] = min(d[j], d[x]+g[x][j]);
//            printf("d[%d] = %d\n", j, d[j]);
        }
    }
}

int main(void) {
    int i, j, t, x, y, n, cnt;
    while(scanf("%d", &n) && (n!=-1)) {
        for(i=0; i<MAXN-10; ++i) {
            g[i][i] = 0;
            for(j=i+1; j<MAXN-10; ++j)
                g[i][j] = g[j][i] = INF;
        }
        
        cnt = 2;
        m.clear();

        cin >> st >> en;
        m[st] = 0;
        m[en] = 1;

        for(i=0; i<n; ++i) {
            cin >> s >> e >> t;
            if(m.find(s) == m.end()) 
                m[s] = cnt++;
            if(m.find(e) == m.end())
                m[e] = cnt++;

            x = m[s];
            y = m[e];
            g[x][y] = g[y][x] = min(g[x][y], t);
        }
        if(st == en) {
            cout << "0" << endl;
            continue;
        }
        /* 
        cout << endl << endl;
        for(it = m.begin(); it!=m.end(); ++it) {
            cout << it->first << " " << it->second << endl;
        }
        */
        Dijstra(0, cnt);
        if(d[1] != INF)
            cout << d[1] << endl;
        else cout << "-1" << endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值