Age of Moyu —— 优先队列+set处理到每个点的最小更换权值数目

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

能用pair的情况下绝对不要用node
用一个优先队列,每次都把最小的值和位置塞进去,因为他是从大到小排的,取个反。
set记录的是相同最小值有多少权值可以到达这个,如果出现了更小的,清空再放。

扫描二维码关注公众号,回复: 2821305 查看本文章
#include<bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define pa pair<int,int>
const int inf=1e9;
struct edge
{
    int to,next,id;
}e[400005];
int head[400005],cnt;

void add(int x,int y,int z)
{
    e[cnt].to=y;
    e[cnt].next=head[x];
    e[cnt].id=z;
    head[x]=cnt++;
}
int val[100005];
set<int>st[100005];
int n,m;
void bfs()
{
    priority_queue<pa>Q;
    Q.push(pa(0,1));
    val[1]=0;
    while(!Q.empty())
    {
        int now=Q.top().second,nowval=-Q.top().first;
        Q.pop();
        if(val[now]<nowval)
            continue;
        for(int i=head[now];~i;i=e[i].next)
        {
            int ne=e[i].to;
            int nval=nowval+(!st[now].count(e[i].id));
            if(val[ne]>nval)
            {
                val[ne]=nval;
                Q.push(pa(-nval,ne));
                st[ne].clear();
                st[ne].insert(e[i].id);
            }
            else if(val[ne]==nval)
                st[ne].insert(e[i].id);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int x,y,z;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        for(int i=1;i<=n;i++)
            val[i]=inf,st[i].clear();
        bfs();
        printf("%d\n",val[n]==inf?-1:val[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/81635433