L3-028 森森旅游(最短路 + multiset)
作者:互联网
思路:
由题意得每次兑换旅行币都要将现金兑完,所以可以将拆解成两段不同的最短路,一条是从1点出发到i点代表用现金的最短路,直接dijkstra即可;另一条则是从i点到达n点的代表用旅行货币的最短路,反向建图再dijkstra即可。
再枚举每个点作为兑换点时从1~n所需要的现金res[i]并加入multise中,每次修改汇率就是将res[i]删掉,再重新计算res[i]加入到multise中,multise.begin()即为每次修改后的现金最小花费值。
但是我这里的代码还有个一分的测试点一直过不了qaq。
代码:
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl
using namespace std;
typedef long long ll;
typedef pair<ll, int> PII;
const int N = 1e5 + 10;
const ll INF = 1e18;
const double eps = 1e-6;
const int mod = 998244353;
int n, m, k;
vector<PII> a[2][N];
int hui[N];
ll dis[2][N];
bool vis[N];
void dijkstra(int idx)
{
memset(vis, false, sizeof vis);
for (int i = 1; i <= n; i++)
dis[idx][i] = INF;
priority_queue<PII, vector<PII>, greater<PII>> pq;
if (idx == 0)
{
pq.push({0, 1});
dis[idx][1] = 0;
}
else
{
pq.push({0, n});
dis[idx][n] = 0;
}
while (pq.size())
{
PII t = pq.top();
pq.pop();
int l = t.second;
ll distance = t.first;
for (auto p : a[idx][l])
{
int r = p.second;
if (dis[idx][r] > distance + p.first)
{
dis[idx][r] = distance + p.first;
pq.push({dis[idx][r], r});
}
}
}
}
int main()
{
fastio;
cin >> n >> m >> k;
while (m--)
{
int l, r, c, d;
cin >> l >> r >> c >> d;
a[0][l].push_back({c, r});
a[1][r].push_back({d, l});
}
for (int i = 1; i <= n; i++)
cin >> hui[i];
dijkstra(0);
dijkstra(1);
multiset<ll> Set;
ll res[N] = {0}; //res[i]为在i点换所要花费的
for (int i = 1; i <= n; i++)
{
res[i] += dis[0][i];
res[i] += (dis[1][i] - dis[1][n]) / hui[i];
if (dis[1][i] % hui[i] != 0)
res[i]++;
Set.insert(res[i]);
}
while (k--)
{
int i, v;
cin >> i >> v;
if (res[i] != 0)
Set.erase(Set.find(res[i]));
hui[i] = v;
res[i] = 0;
res[i] += dis[0][i];
res[i] += (dis[1][i] - dis[1][n]) / hui[i];
if (dis[1][i] % hui[i] != 0)
res[i]++;
Set.insert(res[i]);
cout << *Set.begin() << endl;
}
return 0;
}
标签:pq,idx,int,res,hui,L3,multiset,028,dis 来源: https://blog.csdn.net/Lucky12138/article/details/120544298