Age of Moyu
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1688 Accepted Submission(s): 511
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: 6396 6395 6394 6393 6392
题意:给你一个n个点m条边的图,然后每个边有个颜色,问你从1到n的最短路是多少,规定从一条边到另一条颜色不同的边花费+1.
思路:每次颜色改变花费都会加一,但是从1到这个点有相同花费的路径的颜色可能不一样没这样会影响它下一条更新的点的花费。所以要记录下来到当前点的最短路径的最后一种颜色。用set记录即可,然后跑spfa。
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
struct edge{
int to;
int lenth;
int next;
}a[400020];
int head[200020];
int n,m,q;
int vis[200020];
struct nodope{
int val; //val花费 d结束时的颜色
set<int> d;
}d[200020]; //当前点
int spfa()
{
memset(vis,0,sizeof vis); vis[1]=1;
queue<int> q;
q.push(1);
for(int i=0;i<=n;i++) d[i].val=INF,d[i].d.clear();
d[1].val=0;
while(!q.empty())
{
int u=q.front();q.pop();vis[u]=0;
for(int i=head[u];i!=-1;i=a[i].next)
{
int to=a[i].to;
if(d[to].val>(d[u].val+(d[u].d.find(a[i].lenth)==d[u].d.end())))
{
d[to].val=d[u].val+(d[u].d.find(a[i].lenth)==d[u].d.end());
d[to].d.clear();d[to].d.insert(a[i].lenth);
if(!vis[to])
{
q.push(to);vis[to]=1;
}
}
else if(d[to].val==(d[u].val+(d[u].d.find(a[i].lenth)==d[u].d.end()))&&d[to].d.find(a[i].lenth)==d[to].d.end())
{
d[to].d.insert(a[i].lenth);
if(!vis[to])
{
q.push(to);vis[to]=1;
}
}
}
}
if(d[n].val!=INF)
printf("%d\n",d[n].val);
else printf("-1\n");
return 0;
}
int nn;
void add(int x,int y,int z)
{
a[nn].lenth=z; a[nn].to=y;a[nn].next=head[x];head[x]=nn++;
a[nn].lenth=z; a[nn].to=x;a[nn].next=head[y];head[y]=nn++;
}
int main()
{
//freopen("2.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n;i++)
head[i]=-1;
nn=0;
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);add(x,y,z);
}
spfa();
}
return 0;
}