HDU - 6386 Age of Moyu (搜索)

8 篇文章 0 订阅

Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of NN ports and MM shipping lines. The ports are numbered 11 to NN. Each line is occupied by a Weitian. Each Weitian has an identification number.

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 11 XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of 11 XiangXiangJi. In a case where Mr.Quin changed from some Weitian AA’s line to another Weitian’s line changes to Weitian AA’s line again, the additional cost is incurred again.

Mr.Quin is now at port 11 and wants to travel to port NN where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can’t travel to port NN, print −1−1 instead)

Input

There might be multiple test cases, no more than 2020. You need to read till the end of input.

For each test case,In the first line, two integers NN (2≤N≤100000)(2≤N≤100000) and MM (0≤M≤200000)(0≤M≤200000), representing the number of ports and shipping lines in the city.

In the following m lines, each contain three integers, the first and second representing two ends AiAi and BiBi of a shipping line (1≤Ai,Bi≤N)(1≤Ai,Bi≤N) and the third representing the identification number CiCi (1≤Ci≤1000000)(1≤Ci≤1000000) of Weitian who occupies this shipping line.

Output

For each test case output the minimum required cost. If Mr.Quin can’t travel to port NN, output −1−1 instead.

Sample Input

3 3
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2

Sample Output

1
-1
2

#include <iostream>
#include    <cstdio>
#include   <cstring>
#include     <queue>
#include       <set>
using namespace std;
const int inf=1e9;
struct Edge{
    int next,to,ele;
}arr[400010];
int n,m,cnt,head[400010],dis[100010];
set<int>pd[100010];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

void add_edge(int x,int y,int c)
{
    arr[cnt].next=head[x];
    arr[cnt].to=y;
    arr[cnt].ele=c;
    head[x]=cnt++;
}
void Init(){
    cnt=0;
    memset(head, -1, sizeof(head));
    for(int i=1;i<=n;i++)
    {
        pd[i].clear();
        dis[i]=inf;
    }
}
void solve()
{
    priority_queue<pair<int, int>>p;
    p.push(pair<int,int>(0,1));
    dis[1]=0;
    while (!p.empty()) {
        int now=p.top().second,now_dis=-p.top().first;
        p.pop();
        if(now_dis>dis[now]) continue;
        for(int i=head[now];~i;i=arr[i].next)
        {
            int next_node = arr[i].to;
            int ne_dis = now_dis + (!pd[now].count(arr[i].ele));
            if(ne_dis<dis[next_node])
            {
                dis[next_node] = ne_dis;
                p.push(pair<int, int>(-ne_dis,next_node));
                pd[next_node].clear();
                pd[next_node].insert(arr[i].ele);
            }
            else if(ne_dis==dis[next_node]) pd[next_node].insert(arr[i].ele);
        }

    }

}
int main()
{
    while (scanf("%d%d",&n,&m)==2) {
        Init();
        for(int i=0;i<m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            add_edge(x, y, c);
            add_edge(y, x, c);
        }
        solve();
        if(dis[n]==inf) printf("-1\n");
        else printf("%d\n",dis[n]);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值