zoukankan      html  css  js  c++  java
  • 【天梯L2-001 城市间紧急救援】 双关键字最短路+记录路径 堆优化Dijkstra

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
    输入格式:

    输入第一行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0 ~ (N−1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。

    第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。
    输出格式:

    第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。
    输入样例:

    4 5 0 3
    20 30 40 10
    0 1 1
    1 3 2
    0 3 3
    0 2 2
    2 3 2

    输出样例:

    2 60
    0 1 3

    用Dijkstra跑一下就ok了,双关键字注意更新条件
    用r[i] 记录符合条件的路径上点i的上一个点,输出路径前用栈做一个维护
    Tips:Dijkstra是跑不了有负权边和最长路的

    #include <cstdio>
    #include <stack>
    #include <queue>
    using namespace std;
    
    const int N = 510;
    int l = 0;
    int n;
    int last[N], people[N], r[N];
    bool vis[N] = {false};
    int cnt[N] = {0}, f[N] = {0};
    int pre[N * N], other[N * N], len[N * N];
    
    inline void read(int &x) {
    	x = 0; bool flag = false; char ch;
    	while (ch = getchar(), ch < '!');
    	if (ch == '-') flag = true, ch = getchar();
    	while (x = x * 10 + ch - '0', ch = getchar(), ch > '!');
    	if (flag) x *= -1;
    }
    
    void connect(int x, int y, int z) {
    	l++;
    	pre[l] = last[x];
    	last[x] = l;
    	other[l] = y;
    	len[l] = z;
    }
    
    struct rec{
    	int x, dis, peo;
    	bool operator < (const rec &x) const{
    		if (dis == x.dis) return(peo < x.peo); 
    			else return(dis > x.dis);
    	}
    };
    
    void dijkstra(int s) {
    	int dis[N];
    	priority_queue<rec> que;
    	
    	for (int i = 0; i < n; i++) dis[i] = 0x3f3f3f3f;
    	dis[s] = 0; f[s] = people[s]; cnt[s] = 1;
    	que.push(rec{s, 0, f[s]});
    	//
    	while (!que.empty()) {
    		rec cur = que.top();
    		que.pop();
    		int x = cur.x;
    		if (vis[x]) continue; 
    		vis[x] = true; 
    		int p, q;
    		q = last[x];
    		while (q) {
    			p = other[q];
    			if (dis[p] > dis[x] + len[q]) {
    				dis[p] = dis[x] + len[q];
    				cnt[p] = cnt[x];
    				f[p] = f[x] + people[p];
    				r[p] = x;
    				que.push(rec{p, dis[p], f[p]});
    			} else 
    			if (dis[p] == dis[x] + len[q]) {
    				cnt[p] += cnt[x];
    				if (f[p] < f[x] + people[p]) {
    					f[p] = f[x] + people[p];
    					r[p] = x;
    					que.push(rec{p, dis[p], f[p]});
    				}
    			}
    			q = pre[q];
    		} 
    	}
    	
    }
    
    int main() {
    	int m, ss, st;
    	read(n); read(m); read(ss); read(st);
    	for (int i = 0; i < n; i++) read(people[i]);
    	for (int i = 1; i <= m; i++) {
    		int x, y, z;
    		read(x); read(y); read(z);
    		connect(x, y, z);
    		connect(y, x, z);
    	} 
    	dijkstra(ss);
    	printf("%d %d
    ", cnt[st], f[st]);
    	stack<int> z;
    	
    	while (st != ss) {
    		z.push(st); st = r[st];
    	} 
    	printf("%d", ss);
    	while (!z.empty()) {
    		printf(" %d", z.top());
    		z.pop();
    	}
    	printf("
    ");
    	return 0;
    }
    
  • 相关阅读:
    apiCloud授权绑定第三方账号,微信、QQ、微博。
    Java中JTree的简单案例
    Java中的Swing键盘绑定案例
    Beej网络socket编程指南
    IP处理函数inet_aton()和inet_ntoa(),inet_pton,inet_ntop
    Hadoop开发环境搭建
    Lua和Javascript差异对比
    Lua table pair和ipair区别
    lua中求table长度
    lua metatable和metamethod元表和元方法
  • 原文地址:https://www.cnblogs.com/Eirlys/p/12385915.html
Copyright © 2011-2022 走看看