HDU - 6386 Age of Moyu (搜索)

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]);
    }

}

猜你喜欢

转载自blog.csdn.net/J1nAB1n9/article/details/81668344