codevs1079回家

8 篇文章 0 订阅
4 篇文章 0 订阅

字符版热浪?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int MAXN = 200000+5;
int head[MAXN],tot,n,dis[MAXN],nxt[MAXN <<1];
bool vis[MAXN];
bool mp[MAXN];
struct ware
{
    char s,e;
    int lth;
}a[MAXN];
struct Edge
{
    int from,to,cost;
}edge[MAXN<<1];
void build(int f,int t,int d)
{
    edge[++tot].from = f;
    edge[tot].to = t;
    edge[tot].cost = d;
    nxt[tot] = head[f];
    head[f] = tot;
}
queue <int >q;
void spfa(int s)
{
    memset(dis,0x7f,sizeof(dis));
    vis[s] = 1;
    dis[s] = 0;
    q.push(s);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for(int i = head[x]; i ; i = nxt[i])
        {
            Edge e = edge[i];
            if(dis[e.to] > dis[x] + e.cost)
            {
                dis[e.to] = dis[x] + e.cost;
                if(!vis[e.to])
                {
                    vis[e.to] = 1;
                    q.push(e.to);
                }
            }
        }
    }
}
int main()
{
    char ss;
    scanf("%d",&n);
    for(int i = 1; i <= n; i ++)
    {
        cin>>a[i].s>>a[i].e>>a[i].lth;
        build(a[i].s-'0',a[i].e-'0',a[i].lth);
        build(a[i].e-'0',a[i].s-'0',a[i].lth);
    }
    int ans = 214748364;
    for(int i = 1; i <= n; i ++)
    {
        if(a[i].s <90 && !mp[a[i].s-'0'])
        {
            mp[a[i].s-'0'] = 1;
            spfa(a[i].s-'0');
            if(dis['Z'-'0'] < ans)
            {
                ans = dis['Z'-'0'];
                ss = a[i].s;
            }
        }
        if(a[i].e < 90 && !mp[a[i].e-'0'])
        {
            mp[a[i].e-'0'] = 1;
            spfa(a[i].e-'0');
            if(dis['Z'-'0'] < ans)
            {
                ans = dis['Z'-'0'];
                ss = a[i].e;
            }
        }
    }
    cout <<ss<<" "<< ans<<endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值