L3-028 森森旅游(单源最短路 + multiset 维护多重集最小值

L3-028 森森旅游 (30 分)
思路:
首先不考虑查询和修改操作,只考虑原始图中的答案
把路径分为前半段现金支付和后半段旅游金支付,可以发现去枚举一个决策点,维护 m i n ( c o s t 1 + c o s t 2 ) min(cost_1+cost_2) min(cost1+cost2) 即可
前半段花费显然是单源最短路,后半段建反图也同样是单源最短路
由于后半段是旅游金,需要转化为现金,答案即为 m i n ( c o s t 1 + ( c o s t 2 + a i − 1 ) a i ) min(cost_1+ \frac{(cost_2+a_i-1)}{a_i}) min(cost1+ai(cost2+ai1))
这样我们就可以得到原始图中的最小花费
每次修改是将 a [ x ] a[x] a[x] 修改为 d d d,每次需要插入删除一个元素,我们可以用 m u l t i s e t multiset multiset 维护,为什么是 m u l t i multi multi,因为可能存在多个点的答案相同,而只修改其中一个点时使得这个点的花费更劣时,但答案不变。
注意,如果删除元素 x x x s e . e r a s e ( x ) se.erase(x) se.erase(x) 是将里边所有值为 x x x 的元素删除,而只删除其中一个 x x x 方法为 s e . e r a s e ( s e . f i n d ( x ) ) se.erase(se.find(x)) se.erase(se.find(x))
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 1e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m, q;
struct node{
	int next, to, w;
}e[maxn];
int head[2][maxn], cnt;
ll dis[2][maxn];
bool vis[2][maxn];
int a[maxn];

void add(int x, int y, int z, int op){
	e[++cnt].to = y;
	e[cnt].next = head[op][x];
	e[cnt].w = z;
	head[op][x] = cnt;
}
void dij(int op, int s){
	priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;
	q.push({0, s});
	dis[op][s] = 0;
	while(!q.empty()){
		int now = q.top().second;q.pop();
		if(vis[op][now]) continue;
		vis[op][now] = 1;
		for(int i = head[op][now]; i; i = e[i].next){
			int to = e[i].to;
			if(dis[op][to] > dis[op][now] + e[i].w){
				dis[op][to] = dis[op][now] + e[i].w;
				q.push({dis[op][to], to});
			}
		}
	}
}

void work()
{
	mem(dis, 0x3f);
	cin >> n >> m >> q;
	for(int i = 1; i <= m; ++i){
		int a, b, c, d;cin >> a >> b >> c >> d;
		add(a, b, c, 0);
		add(b, a, d, 1);
	}
	for(int i = 1; i <= n; ++i) cin >> a[i];
	dij(0, 1);
	dij(1, n);
	multiset <ll> se;
	for(int i = 1; i <= n; ++i){
		if(dis[0][i] == INF || dis[1][i] == INF) continue;
			se.insert(dis[0][i] + (dis[1][i] + a[i] - 1) / a[i]);
	}
	while(q--){
		ll x, d;cin >> x >> d;
		if(dis[0][x] == INF || dis[1][x] == INF) {
			cout << *se.begin() << endl;
			continue;
		}
		se.erase(se.find(dis[0][x] + (dis[1][x] + a[x] - 1ll) / a[x]));
		a[x] = d;
		se.insert(dis[0][x] + (dis[1][x] + a[x] - 1ll) / a[x]);
		cout << *se.begin() << endl;
	}
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值