2018hdu杭电多校第七场 hdu6386 Age of Moyu(bfs+优先队列)

Age of Moyu

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

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


题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1;改变至不同编号的路径,花费加1,无论这个编号之前是否走过。

题解:这题看上去是跑一个最短路, 比赛时交了 dijkstra 没加优先队列TLE了很久。赛后听了下题解,发现用最简单的BFS就可以很容易的知道路径,需要比较当前路径种类和上条路径的种类,用一个dis数组去维护起点到每个点直接的最短距离;

ps:每个点可以加入到优先队列中很多次

代码:

#include 
#define INF 0x3f3f3f3f
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for (int i=a;i p.val;
	}
};

int bfs(int x) {
	priority_queue q;
	mem(dis, INF);
	dis[x] = 0;
	q.push(node(x, 0, -1));
	node tmp;
	while (!q.empty()) {
		tmp = q.top();
		q.pop();
		 if (tmp.val > dis[tmp.no]) continue; // 当点权值大与最短路径,舍去这条路径
		 if (tmp.no == n) return tmp.val; // 到是终点了
   		 for (int i = head[tmp.no]; i; i = edges[i].next) {
                        // 这次的状态 与 之前的状态 进行比较
		 	if (dis[tmp.no] + (edges[i].id!=tmp.kind) < dis[edges[i].v]) { 
		 		dis[edges[i].v] = dis[tmp.no] + (edges[i].id!=tmp.kind);
		 		q.push(node(edges[i].v, dis[edges[i].v], edges[i].id));
			}
		 }
	}
	return -1;
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		init();
		if (m == 0) { // 无边的情况
			printf("-1\n");
			continue;
		}
		rep(i, 0, m) {
			scanf("%d%d%d", &u, &v, &id);
			addedge(u, v, id);
			addedge(u, v, id);
		}
		printf("%d\n", bfs(1));
	}
}

 

你可能感兴趣的:(2018多校,题解,hdu)