其他的部分和y总一样,就是最后求最小值的时候用的是stl的优先队列
之前写的代码不知道为啥一直29,所以用y总的代码改了一下
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<LL, int> PII;
const int N = 100010, M = 200010 * 2;
const LL INF = 0x3f3f3f3f3f3f3f3fll;
int n, m, Q;
int h1[N], h2[N], e[M], w[M], ne[M], idx;
LL dist1[N], dist2[N];
bool st[N];
int ratio[N];
LL g[N];
void add(int h[], int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra(int h[], LL dist[], int start)
{
memset(dist, 0x3f, sizeof dist1);
memset(st, 0, sizeof st);
dist[start] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, start});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &Q);
memset(h1, -1, sizeof h1);
memset(h2, -1, sizeof h2);
while (m -- )
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
add(h1, a, b, c), add(h2, b, a, d);
}
for (int i = 1; i <= n; i ++ ) scanf("%d", &ratio[i]);
dijkstra(h1, dist1, 1);
dijkstra(h2, dist2, n);
priority_queue<PII, vector<PII>, greater<PII> > heap;
for (int i = 1; i <= n; i ++ )
if (dist1[i] != INF && dist2[i] != INF)
{
g[i] = dist1[i] + (dist2[i] + ratio[i] - 1) / ratio[i];
//g[i]数组是用来存在i个点金币转换的最小消费的当前值
heap.push({g[i],i});
}
while (Q -- )
{
int a, b;
scanf("%d%d", &a, &b);
if (dist1[a] != INF && dist2[a] != INF)
{
ratio[a] = b;
LL t1 =dist1[a] + (dist2[a] + ratio[a] - 1) / ratio[a];
g[a] = t1;
heap.push({g[a],a});
}
PII t = heap.top();
while (g[t.second] != t.first)
//如果heap里面存的pair对的第二维值对应的g[]当前值和第一维值不等,
//那么这个键值对不符合要求,因为值被更新了
//当一些点的g[]被修改了,我们不好直接从优先队列中删除,
//所以每次判断队头是否和当前g[]相同,不相同则应该删除
{
heap.pop();
t=heap.top();
}
if (Q!=0)
cout<<t.first<<endl;
else cout<<t.first;
}
return 0;
}