HDU 2112 hdu today(巨坑的题)

本文解析了HDU-2112题目中的最短路径问题,探讨了如何利用map进行字符串到数字的映射,并通过Floyd算法求解最短路径。同时分享了在实现过程中遇到的坑点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDU - 2112

我感觉这题没什么好说的,就是坑人啊,这数据怎么想都有问题,他给的n条路里面居然可能不包括起点和终点

别问我怎么知道的,我这么写的代码wa了得十遍,不包括起点和终点,题干你为什么不告诉我一声呢???


然后这道题思路其实不难,如何把字符串转换成数字标记,即把数字与字符串联系起来便于最短路的遍历,不难想到map,如果不是有坑肯定一遍过了。。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
int mapp[162][162];
int cnt;
void init()
{
    for(int i = 1; i <= 160; i ++)
        for(int j = 1; j <= 160; j ++)
    {
        if(i == j)mapp[i][j] = 0;
        else mapp[i][j] = inf;
    }
}
int main()
{
    int n;
    map<string,int>m;
    string a, b;
    int temp;
    int f;
    while(scanf("%d",&n)!=EOF)
    {
        if(n == -1)break;
        cnt = 3;
        f = 0;
        init();
        m.clear();
        cin>>a>>b;
        if(a == b)f=1;
        m[a] = 1;
        m[b] = 2;
        while(n --)
        {
            cin>>a>>b;
            scanf("%d",&temp);
            if(!m[a])
                m[a] = cnt++;
            if(!m[b])
                m[b] = cnt++;
            if(mapp[m[a]][m[b]] > temp)
            mapp[m[b]][m[a]] = mapp[m[a]][m[b]] = temp;
        }
        for(int k = 1; k <= cnt; k ++)
            for(int i = 1; i <= cnt; i ++)
                for(int j = 1; j <= cnt; j ++)
        {
            if(mapp[i][j] > mapp[i][k] + mapp[k][j])
               mapp[i][j] = mapp[i][k] + mapp[k][j];
        }

        if(mapp[1][2] < inf)
        printf("%d\n",mapp[1][2]);
        else if(f == 1)
        printf("0\n");
        else
        printf("-1\n");
    }
    return 0;
}

错误的代码在这里,别进坑了,越想越难受

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
int mapp[205][205];
int cnt;
int n;
map<string,int>m;
string a, b;
string c, d;
int temp;
void init()
{
    for(int i = 1; i <= 200; i ++)
        for(int j = 1; j <= 200; j ++)
    {
        if(i == j)mapp[i][j] = 0;
        else mapp[i][j] = inf;
    }
    cnt = 1;
    m.clear();
}
int main()
{

    while(scanf("%d",&n)!=EOF)
    {
        if(n == -1)break;
        init();
        cin>>c>>d;//坑在这里
        while(n --)
        {
            cin>>a>>b;
            cin>>temp;
            if(m[a] == 0)
                m[a] = cnt++;
            if(m[b] == 0)
                m[b] = cnt++;
            if(mapp[m[a]][m[b]] > temp)
            mapp[m[b]][m[a]] = mapp[m[a]][m[b]] = temp;
        }
        for(int k = 1; k < cnt; k ++)
            for(int i = 1; i < cnt; i ++)
                for(int j = 1; j < cnt; j ++)
        {
            if(mapp[i][j] > mapp[i][k] + mapp[k][j])
               mapp[i][j] = mapp[i][k] + mapp[k][j];
        }
        if(mapp[m[c]][m[d]] < inf)
        cout<<mapp[m[c]][m[d]]<<endl;
        else
        cout<<-1<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值