CF853B

http://www.elijahqi.win/archives/762
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.

There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.

You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.

Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for kdays and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.

Input
The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106).

The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106,0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.

Output
Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.

If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output “-1” (without the quotes).

Examples
input
2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
output
24500
input
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000
output
-1
Note
The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.

In the second sample it is impossible to send jury member from city 2 back home from Metropolis.

首先先大致阐述下题意:n个城市都要到城市0(首都)开会,开会时长为k天,而且掐头去尾k天 可以停留超过k天

我们要选择总机票花费最小来做

我们预处理两个数组dp[i]&dp1[i]分别表示

dp[i]表示截止到第i天所有代表已经抵达首都的最小花费

dp1[i]表示从第i天开始陆续有代表离开,并且一定在最后一天前所有代表都离开了的最小花费

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 1100000
#define inf 1LL<<60
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch<='9'&&ch>='0') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node{
    int t,de,te,c;
}data[N];
inline bool cmp(node a,node b){
    return a.t<b.t;
}
int min1[N],s,n,m,k,t;
long long dp[N],dp1[N],ans;
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();k=read();
    for (int i=1;i<=m;++i){data[i].t=read();data[i].de=read();data[i].te=read();data[i].c=read();t=max(t,data[i].t);}
    sort(data+1,data+m+1,cmp);
    for (int i=1;i<=t;++i) dp[i]=inf;
    int tot=0;long long sum=0;
    for (int i=1;i<=m;++i){
        if (data[i].de==0) continue;
        int de=data[i].de,cost=data[i].c;
        if (min1[de]==0) min1[de]=cost,tot++,sum+=cost;else
        if(cost<min1[de]) sum-=min1[de],min1[de]=cost,sum+=cost;
        if (tot==n) dp[data[i].t]=sum;
    } s=1;
    while (s<t-1&&dp[s]==inf) ++s;
    if (s>=t-1) {printf("-1");return 0;}
    for (int i=s+1;i<=t-2;++i) dp[i]=min(dp[i],dp[i-1]);
    for (int i=1;i<=t;++i) dp1[i]=inf;
    tot=sum=0;memset(min1,0,sizeof(min1));
    for (int i=m;i>=1;--i){
        if (data[i].te==0) continue;
        int te=data[i].te,cost=data[i].c;
        if (min1[te]==0) min1[te]=cost,tot++,sum+=cost;else
        if (cost<min1[te]) sum-=min1[te],min1[te]=cost,sum+=cost;
        if (tot==n) dp1[data[i].t]=sum;
    }s=t;
    while (s>0&&dp[s]==inf)--s;
    if (s<1){printf("-1");return 0;}
    for (int i=s-1;i>=1;--i) dp1[i]=min(dp1[i],dp1[i+1]);
//  for (int i=1;i<=t;++i) printf("%d ",dp[i]);printf("\n");
//  for (int i=1;i<=t;++i) printf("%d ",dp1[i]);printf("\n");
    ans=inf;
    for (int i=1;i+k+1<=t;++i){
        if (dp[i]==inf||dp1[i+k+1]==inf) continue;
        ans=min(ans,dp[i]+dp1[i+k+1]);
    }
    if (ans==inf) printf("-1");else printf("%lld",ans);
    return 0;
}
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
### 关于 Codeforces Problem CF175B 的解决方案 #### 问题描述 Codeforces Problem CF175B 是一个涉及路径规划和字符串操作的问题。题目要求设计一个机器人程序,使其能够按照给定的指令完成特定的任务。 虽然当前未提供具体题目的详细说明,但从常见的机器人路径问题来看,通常会涉及到以下几个方面: - **输入解析**:读取机器人的初始位置以及目标位置。 - **路径计算**:通过分析可能的方向变化来构建最短路径。 - **输出生成**:将路径转换为一系列字符表示的动作序列。 以下是基于常见机器人路径问题的一个通用解决框架[^3]: --- #### 解决方案概述 为了实现该问题的目标,可以采用如下策略: 1. 定义方向向量 `dx` 和 `dy` 来表示上下左右四个移动方向。 2. 使用广度优先搜索(BFS)算法找到从起点到终点的最短路径。 3. 将 BFS 路径转化为对应的命令字符串形式。 下面是具体的代码实现示例: ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 100; char grid[MAX_N][MAX_N]; bool visited[MAX_N][MAX_N]; // 方向定义 struct Direction { int dx, dy; char command; }; Direction directions[] = {{0, 1, 'R'}, {0, -1, 'L'}, {1, 0, 'D'}, {-1, 0, 'U'}}; int num_directions = sizeof(directions) / sizeof(directions[0]); void bfs(int startX, int startY, int endX, int endY, vector<char>& path) { queue<pair<int, pair<int, string>>> q; // (current_x, current_y), commands_so_far q.push({startX, {startY, ""}}); visited[startX][startY] = true; while (!q.empty()) { auto front = q.front(); q.pop(); int cx = front.first; int cy = front.second.first; string cmd = front.second.second; if (cx == endX && cy == endY) { for (auto c : cmd) path.push_back(c); return; } for (int i = 0; i < num_directions; ++i) { int nx = cx + directions[i].dx; int ny = cy + directions[i].dy; if (nx >= 0 && nx < MAX_N && ny >= 0 && ny < MAX_N && !visited[nx][ny] && grid[nx][ny] != '#') { visited[nx][ny] = true; q.push({nx, {ny, cmd + directions[i].command}}); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; pair<int, int> start, finish; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> grid[i][j]; if (grid[i][j] == 'S') start = {i, j}; if (grid[i][j] == 'E') finish = {i, j}; } } memset(visited, false, sizeof(visited)); vector<char> resultPath; bfs(start.first, start.second, finish.first, finish.second, resultPath); cout << resultPath.size() << "\n"; for (auto c : resultPath) cout << c; cout << "\n"; return 0; } ``` 上述代码实现了以下功能: - 利用 BFS 寻找从起点到终点的最短路径。 - 输出路径长度及其对应的操作序列。 --- #### 复杂度分析 - 时间复杂度:O(N * M),其中 N 表示网格的高度,M 表示宽度。这是由于 BFS 需要遍历整个地图一次。 - 空间复杂度:O(N * M),用于存储访问状态矩阵和队列中的节点信息。 --- #### 注意事项 如果实际问题中有额外约束条件,则需调整 BFS 或增加剪枝逻辑以优化性能。例如,在某些情况下,可以通过启发式函数改进搜索效率(A* 算法)。此外,还需注意边界情况处理,比如无法到达目的地的情形应返回特殊标志或错误提示[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值