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

11 篇文章 0 订阅
9 篇文章 0 订阅

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 <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for (int i=a;i<n;i++)
using namespace std;
//------------------------------------head------------------------------------
const int N = 2e5 + 5;

int head[N], tot;
int dis[N]; // 记录起点到所有点的最短距离
int n, m;
int u, v, id;

struct edge{  // 邻接表
	int v, id, next;
} edges[N<<2]; // 等价于 N*4

void init() {
	tot = 0; // 记的边数
	mem(head, 0);
}

void addedge(int u, int v, int id) { // 加边
	edges[++tot] = (edge){v, id, head[u]};
	head[u] = tot;
	edges[++tot] = (edge){u, id, head[v]};
	head[v] = tot;
}

struct node {
	int no, val, kind; // 边的序号,边的权值,边的种类
	node() {}
	node(int _no, int _val, int _kind) : no(_no), val(_val), kind(_kind) {}
	bool operator < (const node &p) const {  //用于优先队列的排序 权值小的在堆顶
		return val > p.val;
	}
};

int bfs(int x) {
	priority_queue<node> 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));
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值