Age of Moyu HDU - 6386

Age of Moyu

HDU - 6386

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

The i-th (1≤i≤M) line connects port Ai and Bi (Ai≠Bi) bidirectionally, and occupied by Ci Weitian (At most one line between two ports).

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 1 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 1 XiangXiangJi. In a case where Mr.Quin changed from some Weitian A's line to another Weitian's line changes to Weitian A's line again, the additional cost is incurred again.

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

instead)

Input

There might be multiple test cases, no more than 20

. You need to read till the end of input.

For each test case,In the first line, two integers N (2≤N≤100000) and M (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 Ai and Bi of a shipping line (1≤Ai,Bi≤N) and the third representing the identification number Ci (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 N

, output −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

AC代码

//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (l+r)/2
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
//typedef unsigned long long ull;
#define MOD 142857
const int maxn = 100100;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int n, m, s, t;
int len;
struct road
{
	int u, v, len;
	int next;
	void getvalue(int uu, int vv, int ll, int nn)
	{
	    u = uu; v = vv; len = ll; next = nn;
	}
	road(){}
};
struct node
{
    int v;
    int len;
    int cost;
    node(int v, int l, int c):v(v),len(l),cost(c){}
    friend bool operator <(node a, node b)
    {
        return a.cost>b.cost;
    }
};
road G[maxn*5];
int h[maxn];
int d[maxn];
bool vis[maxn];
set<pair<int, int> > st;
int bfs()
{
    ms(vis, 0);
    st.clear();
    fill(d, d+n+1, INF);
    d[1] = 0;
    priority_queue<node> pque;
    pque.push(node(1, 0, 0));
    st.insert(mp(1,0));
    while(pque.size())
    {
        node p = pque.top(); pque.pop();
        if(d[p.v] < p.cost) continue;

        for(int i = h[p.v]; i != -1; i = G[i].next)
        {
            road e = G[i];
            if(e.len == p.len)
            {
                if(d[e.v] > d[e.u])
                {
                    d[e.v] = d[e.u];
                    pque.push(node(e.v,e.len,p.cost));
                }
            }
            else
            {
                if(d[e.v] > d[e.u] + 1)
                {
                    d[e.v] = d[e.u] + 1;
                    pque.push(node(e.v,e.len,p.cost+1));
                }
            }
        }
    }
    if(d[n] == INF)
    return -1;
    else
    return d[n];
}
int main()
{
	//freopen("out.txt", "w", stdout);
    while(~sdd(n, m))
    {
        int ta, tb, tc;
        len = 0;
        fill(h, h+n+1, -1);
        for(int i = 0; i < m; i++)
        {
            sddd(ta, tb, tc);
            G[len].getvalue(ta,tb,tc,h[ta]);
            h[ta] = len++;

            G[len].getvalue(tb,ta,tc,h[tb]);
            h[tb] = len++;
        }
        int ans = bfs();

        printf("%d\n", ans);
    }

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值