• HDU 6386 Age of Moyu


    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 (1iM) line connects port Ai and Bi (AiBi) 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 (2N100000) and M (0M200000), 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 (1Ai,BiN) and the third representing the identification number Ci (1Ci1000000) 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

     

    题解:

    最短路的写法;始终去找最小费用,保存上一次的边权和这一次比较,然后查看应当+1还是+0

    我是根据https://blog.csdn.net/m0_37611893/article/details/81636688 这个链接而写,

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN=100010;
    const int INF=0x3f3f3f3f;
    int n,m;
    struct qnode{
        int v;   //下一个点
        int c;   //当前花费
        int w;   //当前这条边的价值
        qnode(int _v = 0, int _c = 0, int _w = 0):v(_v),c(_c),w(_w){}
        bool operator < (const qnode &r) const{    //按照费用小的在前
            return c>r.c;
        }
    };
    struct edge{
        int v,cost;
        edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
    };
    vector<edge>E[MAXN];
    int dis[MAXN];
    int vis[MAXN];
    void dij()
    {
        for (int i = 0; i <=n ; ++i) {
            dis[i]=INF;
        }
        memset(vis, false, sizeof(vis));
        priority_queue<qnode>q;
        while(!q.empty()) q.pop();
        dis[1]=0;
        q.push(qnode(1,0,0));
        qnode tmp;
        while (!q.empty())
        {
            tmp=q.top();q.pop();
            int u=tmp.v;
            if(vis[u]) continue;
            vis[u]=true;
            dis[u]=tmp.c;
            for (int i = 0; i <E[u].size() ; ++i) {
                int v=E[u][i].v;
                int w1=E[u][i].cost;
                if(!vis[v])
                {
                    int temp;
                    if(tmp.w!=w1) temp=tmp.c+1;//如果这个边的值和上一条边值不一样,费用+1;
                    else temp=tmp.c;
                    q.push(qnode(v,temp,w1));
                }
            }
        }
    
    }
    int main()
    {
        while (scanf("%d%d",&n,&m)!=EOF)
        {
            for (int i = 0; i <=n ; ++i) {
                E[i].clear();
            }
            int a,b,c;
            for (int i = 0; i <m ; ++i) {
                scanf("%d%d%d",&a,&b,&c);
                E[a].push_back(edge(b,c));
                E[b].push_back(edge(a,c));
            }
            dij();
            if(dis[n]==INF) printf("-1
    ");
            else printf("%d
    ",dis[n]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    java9新特性-9-语法改进:try语句
    10.04 FZSZ模拟Day1 总结
    10.03模拟总结
    HNOI2012 永无乡
    ZJOI2007 报表统计
    HNOI2004 宠物收养场
    HNOI2002 营业额统计
    Splay 区间反转
    Splay基本操作
    HEOI2016 树
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9482400.html
Copyright © 2020-2023  润新知