HDU 2112 HDU Today

哭

这是个非常忧伤的题。

且不说 他的样例输出 欺骗了我。

害的我去输出 路径。

这特么居然是个 无向图…………

无向图就算了。


C++ AC。。。。

G++超时是要闹哪样!


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<iostream>
#define INF 0xfffffff
using namespace std;
int m;
struct lx
{
    int v,t;
};
vector<lx>g[155];
map<string,int>str;

int dis[155];
bool vis[155];
void SPFA(int start,int thend)
{
    for(int i=0;i<155;i++)
        dis[i]=INF,vis[i]=0;
    dis[start]=0,vis[start]=1;
    queue<int>q;
    q.push(start);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int j=0;j<g[u].size();j++)
        {
            int v=g[u][j].v;
            int t=g[u][j].t;
            if(dis[v]>dis[u]+t)
            {
                dis[v]=dis[u]+t;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(dis[thend]>=INF)
        puts("-1");
    else
        printf("%d\n",dis[thend]);
}
int main()
{
    while(scanf("%d",&m)!=EOF)
    {
        if(m==-1)return 0;
        str.clear();
        for(int i=0;i<155;i++)
            g[i].clear();
        int cot=1;
        int u,v,t;
        int start,thend;
        string s1,s2;
        cin>>s1>>s2;
        if(str[s1]==0)
            str[s1]=cot++;
        start=str[s1];
        if(str[s2]==0)
            str[s2]=cot++;
        thend=str[s2];
        while(m--)
        {
            cin>>s1>>s2>>t;
            if(str[s1]==0)
                str[s1]=cot++;
            u=str[s1];
            if(str[s2]==0)
                str[s2]=cot++;
            v=str[s2];
            lx now;
            now.t=t;
            now.v=v;
            g[u].push_back(now);
            now.v=u;
            g[v].push_back(now);
        }
        SPFA(start,thend);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值