准备PAT之Public Bike Management——DFS/DFS+Dijikstra

本文介绍了一个基于深度优先搜索(DFS)和迪杰斯特拉算法的解决方案,用于解决城市公共自行车调度问题。目标是最优地调整各个站点的自行车数量,使其达到半满的理想状态。文章详细解释了算法的设计思路及实现步骤。

Public Bike Management (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

Figure 1

Figure 1 illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:
1. PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.
2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

输入描述:
Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci (i=1,…N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.

输出描述:
For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0->S1->…->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.
Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge’s data guarantee that such a path is unique.

输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

输出例子:
3 0->2->3 0
DFS方法

//若求得是有多条最短路径且根据要求选择,可用sort()+cmp()进行排序
//但若要对路径上的点进行操作,我觉的DFS比Dijikstra要方便
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int max_bike = 505;
const int INF = 0x3f3f3f3f;

int cost[max_bike][max_bike];
int cur_cost_time, cur_back_bike, cur_take_bike;
int full_bike, station_num, sp, road_num;
int if_visit[max_bike];
int min_back_bike, min_take_bike, min_cost_time;
int station_bike[max_bike];
vector<int> short_path;
vector<int>cur_path;

void Init() {
    for (int i = 0; i <= station_num; i++) {
        for (int j = 0; j <= station_num; j++) {
            cost[i][j] = INF;
        }
    }
    memset(if_visit, 0, sizeof(if_visit));
    cur_cost_time = cur_back_bike = cur_take_bike = 0;
    min_cost_time = min_back_bike = min_take_bike = INF;
}
void Input_and_init() {
    cin >> full_bike >> station_num >> sp >> road_num;
    Init();
    for (int i = 1; i <= station_num; i++) {
        cin >> station_bike[i];
    }
    for (int i = 0; i < road_num; i++) {
        int from, to, _cost;
        cin >> from >> to >> _cost;
        cost[from][to] = cost[to][from] = _cost;
    }
}
void Dfs(int cur_station) {//当前的站点不压入堆中
                                                     //可不可以计算当前的点而不是计算下一个
    if (cur_cost_time > min_cost_time)  return;

    if (cur_station == sp) {
        if (cur_cost_time < min_cost_time) {
            min_cost_time = cur_cost_time;
            min_back_bike = cur_back_bike;
            min_take_bike = cur_take_bike;
            short_path = cur_path;
        }
        else {
            if (cur_take_bike < min_take_bike || 
                (cur_take_bike == min_take_bike&&cur_back_bike < min_back_bike)) {
                min_take_bike = cur_take_bike;
                min_back_bike = cur_back_bike;
                short_path = cur_path;
            }
        }
        return;
    }

    for (int i = 1; i <= station_num; i++) {
        if (if_visit[i] || cost[cur_station][i] == INF) continue;

        int _cur_back = cur_back_bike, _cur_take = cur_take_bike;

        if (cur_back_bike + station_bike[i] < full_bike / 2) {
            cur_take_bike += full_bike / 2 - cur_back_bike - station_bike[i];
            cur_back_bike = 0;
        }
        else {
            cur_back_bike += station_bike[i] - full_bike / 2;
        }

        if_visit[cur_station] = 1;
        cur_path.push_back(i);
        cur_cost_time += cost[cur_station][i];
        Dfs(i);
        cur_path.pop_back();
        if_visit[cur_station] = 0;//漏了,找了将近半个小时,得出->先先检查回溯,再看过程
        cur_cost_time -= cost[cur_station][i];
        cur_back_bike = _cur_back; cur_take_bike = _cur_take;
    }
}
void Output() {
    cout << min_take_bike << " 0";
    for (int i = 0; i < short_path.size(); i++) {
        cout << "->" << short_path[i];
    }
    cout << " " << min_back_bike << endl;
}
int main() {
    Input_and_init();
    Dfs(0);
    Output();
    return 0;
}

DFS()+Dijikstra方法

#include<algorithm>
#include<vector>
#include<queue>
#include<fstream>
#include<iostream>
using namespace std;
const int INF = 100000000;
const int max_bike = 505;
int bike_max, num_station, sp, num_road;

int cost[max_bike][max_bike];
int short_dist[max_bike];
int if_visit[max_bike];
int station_bike[max_bike];

struct Prev {
    vector<int> prev_station;
    int take, back;
};
vector<Prev> _prev;
Prev temp;

void Init() {
    fill(short_dist, short_dist + num_station + 3, INF);
    fill(if_visit, if_visit + num_station + 3, 0);
    for (int i = 0; i <= num_station; i++)
        for (int j = 0; j <= num_station; j++)
            cost[i][j] = INF;
    short_dist[0] = 0;
}
void Input() {
    cin >> bike_max >> num_station >> sp >> num_road;
    Init();
    for (int i = 1; i <= num_station; i++) {
        cin >> station_bike[i];
    }
    for (int i = 0; i < num_road; i++) {
        int _from, _to, _cost;
        cin >> _from >> _to >> _cost;
        cost[_from][_to] = cost[_to][_from] = _cost;
    }
}
void Dijiksta(int s) {

    while(true) {
        int v = -1; 
        for (int i = 0; i <= num_station; i++) {
            if (!if_visit[i] && (v == -1 || short_dist[i] < short_dist[v])) {
                v = i;
            }
        }
            if (v == -1)    break;

        if_visit[v] = 1;
        for (int i = 0; i <= num_station; i++) {
            if (short_dist[i] > short_dist[v] + cost[v][i]) {
                short_dist[i] = short_dist[v] + cost[v][i];
            }
        }
    }
}
void Dfs(int cur_station, int path_cost) {
    if_visit[cur_station] = 1;
    //此处不把0压入
    if (cur_station) temp.prev_station.push_back(cur_station);

    if (path_cost > short_dist[sp]) return;
    if (cur_station == sp) {
        if (path_cost == short_dist[sp])
            _prev.push_back(temp);
        return;
    }

    for (int i = 0; i <= num_station; i++) {
        if (!if_visit[i] && cost[cur_station][i] != INF) {
            Dfs(i, path_cost + cost[cur_station][i]);
            if_visit[i] = 0;
            temp.prev_station.pop_back();
        }
    }
}
bool cmp(const Prev& a,const Prev& b){
    if(a.take==b.take)
        return a.back < b.back;
    return a.take < b.take;
}
void Output() {
    sort(_prev.begin(), _prev.end(), cmp);
    cout << _prev[0].take<<" "<<0;
    for (int i = 0; i < _prev[0].prev_station.size(); i++) {
        cout << "->" << _prev[0].prev_station[i];
    }
    cout << " " << _prev[0].back << endl;
}
void Fix_flag() {
    for (int i = 0; i <= num_station; i++) {
        if_visit[i] = 0;
    }
}
void Calculater() {//计算各点对自行车的影响
    for (int i = 0; i < _prev.size(); i++) {
        int need_back = 0; int need_take = 0;
        for (int j = 0; j <_prev[i].prev_station.size(); j++) {
            int n = _prev[i].prev_station[j];
            if (station_bike[n] + need_back <= bike_max / 2) {
                need_take += bike_max / 2 - station_bike[n] - need_back;
                need_back = 0;
            }
            else {
                need_back += station_bike[n] - bike_max / 2;
            }
        }
        _prev[i].back = need_back;
        _prev[i].take = need_take;
    }
}
int main() {
    Input();
    Dijiksta(0);
    Fix_flag();//为了DFS对if_visit进行清零
    Dfs(0, 0);
    Calculater();
    Output();
    return 0;
}
基于TROPOMI高光谱遥感仪器获取的大气成分观测资料,本研究聚焦于大气污染物一氧化氮(NO₂)的空间分布与浓度定量反演问题。NO₂作为影响空气质量的关键指标,其精确监测对环境保护与大气科学研究具有显著价值。当前,利用卫星遥感数据结合先进算法实现NO₂浓度的高精度反演已成为该领域的重要研究方向。 本研究构建了一套以深度学习为核心的技术框架,整合了来自TROPOMI仪器的光谱辐射信息、观测几何参数以及辅助气象数据,形成多维度特征数据集。该数据集充分融合了不同来源的观测信息,为深入解析大气中NO₂的时空变化规律提供了数据基础,有助于提升反演模型的准确性与环境预测的可靠性。 在模型架构方面,项目设计了一种多分支神经网络,用于分别处理光谱特征与气象特征等多模态数据。各分支通过独立学习提取代表性特征,并在深层网络中进行特征融合,从而综合利用不同数据的互补信息,显著提高了NO₂浓度反演的整体精度。这种多源信息融合策略有效增强了模型对复杂大气环境的表征能力。 研究过程涵盖了系统的数据处理流程。前期预处理包括辐射定标、噪声抑制及数据标准化等步骤,以保障输入特征的质量与一致性;后期处理则涉及模型输出的物理量转换与结果验证,确保反演结果符合实际大气浓度范围,提升数据的实用价值。 此外,本研究进一步对不同功能区域(如城市建成区、工业带、郊区及自然背景区)的NO₂浓度分布进行了对比分析,揭示了人类活动与污染物空间格局的关联性。相关结论可为区域环境规划、污染管控政策的制定提供科学依据,助力大气环境治理与公共健康保护。 综上所述,本研究通过融合TROPOMI高光谱数据与多模态特征深度学习技术,发展了一套高效、准确的大气NO₂浓度遥感反演方法,不仅提升了卫星大气监测的技术水平,也为环境管理与决策支持提供了重要的技术工具。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值