zoukankan      html  css  js  c++  java
  • hdu6386 Age of Moyu【最短路】

    Age of Moyu

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 3125    Accepted Submission(s): 981


     

    Problem Description

    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 −1instead)

     

    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

     

    Source

    2018 Multi-University Training Contest 7

     

    Recommend

    chendu   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404 

     

    其实当时没怎么认真思考这道题要怎么写

    补题时候看的题解

    有个题解说用set 其实没怎么搞清楚要怎么写 但是这种方法也可以实现

    用优先队列优化dijkstra 队列维护的是边而不是点

    根据当前边的编号决定cost是否增加

    因为是优先队列所以可以保证最先到达n的线路是cost最小的

    好像比赛的时候他们写T了好多次....不知道他们是怎么写的

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int n, m;
    const int maxn = 100005;
    const int maxm = 200005;
    int head[maxn], cnt;
    struct edge{
        int v, f, w;
        int nxt;
        bool operator < (edge a) const{
            return w > a.w;
        }
    }e[maxm * 2];
    
    void addedge(int u, int v, int f)
    {
        e[cnt].v = v;
        e[cnt].f = f;
        e[cnt].nxt = head[u];
        head[u] = cnt;
        cnt++;
    }
    
    int dis[maxn], vis[maxn];
    void init()
    {
        cnt = 0;
        memset(vis, 0, sizeof(vis));
        memset(dis, inf, sizeof(dis));
        memset(head, -1, sizeof(head));
    }
    
    void dijkstra()
    {
        dis[1] = 0;
        priority_queue <edge> q;
        while(!q.empty()){
            q.pop();
        }
        edge now;
        now.v = 1;
        now.f = 0;
        now.w = 0;
        q.push(now);
        while(!q.empty()){
            now = q.top();
            q.pop();
            int u = now.v;
            if(vis[u]){
                continue;
            }
            vis[u] = true;
            dis[u] = now.w;
            for(int i = head[u]; ~i; i = e[i].nxt){
                int v = e[i].v;
                if(!vis[v]){
                    edge nex;
                    nex.v = v;
                    nex.f = e[i].f;
                    nex.w = dis[u] + (e[i].f != now.f);
                    q.push(nex);
                }
            }
        }
    }
    
    int main()
    {
    	while(scanf("%d%d", &n, &m) != EOF){
            init();
    
            for(int i = 0; i < m; i++){
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                addedge(a, b, c);
                addedge(b, a, c);
            }
    
            dijkstra();
            if(dis[n] == inf){
                printf("-1
    ");
            }
            else{
                printf("%d
    ", dis[n]);
            }
    	}
    	return 0;
    }
    
  • 相关阅读:
    解决编程开发时候文件命名的苦恼(规范代码 提高可读性)
    react的this.setState没有触发render
    动态修改JS对象的值及React setState
    Antd Select组件结合使用出现must set key for <rc-animate> children问题
    antd Select进阶功能 动态更新、函数防抖
    前端性能优化之重排和重绘
    ES5 map循环一大坑:循环遍历竟然出现逗号!
    Monent.js:强大的日期处理类库
    Docker概览
    Spring boot 集成hessian
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643382.html
Copyright © 2011-2022 走看看