2018 Multi-University Training Contest 7 HDU6386 Age of Moyu (bfs+优先队列+set)

Age of Moyu Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2323 Accepted Submission(s): 701

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

Source
2018 Multi-University Training Contest 7

题解:
1.题目意思为从1->n的最短路径,但是有个限制,如果连续的边编号一致时只算一个距离单位。
2.直接用bfs加优先队列加set优化(相当于dijkstra堆优化写法的变形)
3.set主要是为了优化掉同一种前置标号的情况
4.注意优先队列结构体重载<的写法

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e5  + 100;
const int maxm = 4e5 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long LL;
//const LL mod = 1e9 + 7;

#define PI 3.1415926
#define sc(x)  scanf("%d",&x)
#define pf(x)  printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%I64d\n",x)
#define pfs(x) printf("%d ",x)
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb(x)  push_back(x);

void read(int &x){
	char ch = getchar();x = 0;
	for (; ch < '0' || ch > '9'; ch = getchar());
	for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}

int n,m,tot;
int d[maxn];
int head[maxn];
set<int> cnt[maxn];

struct edge{
	int to,id,next;
	edge(int to = 0, int id = 0, int next = 0) : to(to),id(id),next(next){}
}es[maxm];

void add_edge(int u,int v,int id){
	es[tot] = edge(v,id,head[u]);
	head[u] = tot++;
}

struct Node{
	int u,d,pre,fa;
	Node(int u = 0, int d = 0, int pre = 0,int fa = 0) : u(u),pre(pre),d(d),fa(fa){}
	bool operator < (const Node &a) const{return d > a.d;}
	//对于优先队列从小到大输出
};

void bfs(int s, int t)
{
	priority_queue<Node> Q;
	rep(i,1,n+1) cnt[i].clear();
	mem(d,INF);
	d[s] = 0;
	Q.push(Node(s,0,-1,-1));
	while(!Q.empty())
	{
		Node x = Q.top();Q.pop();
		if(d[x.u] < x.d) continue;
    if(cnt[x.u].find(x.pre) != cnt[x.u].end()) continue;
		else cnt[x.u].insert(x.pre);
		for(int i = head[x.u]; ~i; i = es[i].next)
		{
			edge e = es[i];
			if(x.fa == e.to) continue;
			if(d[e.to] >= d[x.u] + (x.pre != e.id))
			{
				d[e.to] = d[x.u] + (x.pre != e.id);
				if(e.to != t) Q.push(Node(e.to,d[e.to],e.id,x.u));
			}
		}
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		tot = 0;
		int u,v,id;
		mem(head,-1);
		while(m--){scanf("%d%d%d",&u,&v,&id);add_edge(u,v,id);add_edge(v,u,id);}
		bfs(1,n);
		if(d[n] == INF) d[n] = -1;
		pfn(d[n]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值