hdu 1546 Idiomatic Phrases Game (spfa)

读取的字符串一定要开的大一点啊哭哭哭不然就是无尽的RE

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

const int N = 1005;
const int inf = 1 << 28;

struct pp{
    int st, ed, val;
}p[N];

struct node{
    int nxt, to, w;
}e[N*N];

int head[N], vis[N], dis[N];
int n, cnt;

void add( int u, int v, int w )
{
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void build()
{
    memset(head, -1, sizeof(head));
    cnt = 0;
    for( int i = 1; i <= n; i++ )
    {
        for( int j = 1; j <= n; j++ )
        {
            if( p[i].ed == p[j].st && i != j )
            {
                add(i, j, p[i].val);
            }
        }
    }
}

void spfa()
{
    queue<int> q;
    while( !q.empty() )
        q.pop();
    for( int i = 1; i <= n; i++ )
    {
        dis[i] = inf;
        vis[i] = 0;
    }
    q.push(1);
    dis[1] = 0;
    vis[1] = 1;
    while( !q.empty() )
    {
        int now = q.front();
        q.pop(), vis[now] = 0;
        for ( int i = head[now]; ~i; i = e[i].nxt )
        {
            int to = e[i].to;
            if( dis[to] > dis[now] + e[i].w )
            {
                dis[to] = dis[now] + e[i].w;
                if( !vis[to] )
                {
                    vis[to] = 1;
                    q.push(to);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d", &n) && n )
    {
        char a[200];
        for ( int i = 1; i <= n; i++ )
        {
            scanf("%d", &p[i].val);
            scanf("%s", a+1);
            int s = 0, e = 0;
            for( int j = 1; j <= 4; j++ )
            {
                if( a[j] >= '0' && a[j] <= '9' )
                    s = s * 10 + a[j] - '0';
                else
                    s = s * 10 + a[j] - 'A' + 10;
            }
            for( int j = strlen(a+1) - 3; j <= strlen(a+1); j++ )
            {
                if( a[j] >= '0' && a[j] <= '9' )
                    e = e * 10 + a[j] - '0';
                else
                    e = e * 10 + a[j] - 'A' + 10;
            }
            //printf("%d %d\n", s, e);
            p[i].st = s, p[i].ed = e;
        }
        build();
        spfa();
        printf("%d\n", dis[n] == inf? -1: dis[n]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值