怎么把CSP-S阅读程序的非严格改成严格啊
  • 板块学术版
  • 楼主wbh20090611
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/9/23 20:29
  • 上次更新2024/9/23 22:02:58
查看原帖
怎么把CSP-S阅读程序的非严格改成严格啊
573963
wbh20090611楼主2024/9/23 20:29

rt,

#include <cstdio>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
const int maxn = 2e5+10, maxm =1e6+10, inf = 522133279;
int n, m, s, t;
int head[maxn], nxt[maxm], to[maxm], w[maxm], tot = 1;
int dis[maxn<<1], *dis2;
int pre[maxn<<1], *pre2;
bool vis[maxn<<1];
void add(int a, int b, int c)
{
	++tot;
	nxt[tot] = head[a];
	to[tot] = b;
	w[tot] = c;
	head[a] = tot;
}
bool upd(int a, int b, int d, priority_queue<pair<int, int> >&q)
{
	if (d >= dis[b])
		return false;
	if (b < n)
		upd(pre[b], n + b, dis[b], q);
	q.push(make_pair(-d, b)); 
	dis[b] = d;
	pre[b] = a;
	return true;
}
void solve(){
	priority_queue<pair<int, int> >q;
	q.push(make_pair(0, s));
	memset(dis, 0x1f,sizeof(dis));
	memset(pre, -1, sizeof(pre));
	dis2 = dis+n;
	pre2 = pre+n; 
	dis[s] = 0;
	while (!q.empty()){
		int aa = q.top().second; q.pop();
		if (vis[aa]) continue;
		vis[aa] = true;
		int a = aa %n;
		for (int e = head[a]; e; e = nxt[e]){
			int b= to[e], c= w[e];
			if (aa < n) {
				if (!upd(a, b, dis[a]+c, q))
					upd(n,n+b,dis[a]+c,q);
			}
			else upd(n+a,n+b,dis2[a]+c,q);
		}
		
	}
}
int main(){
	scanf("%d%d", &n, &m);
	s = 0;
	t = n - 1;
	for(int i=0;i< m;++i){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		add(a-1, b-1, c);
		add(b-1, a-1, c);
	}
	solve();
	printf("%d\n", dis2[t]);
}
2024/9/23 20:29
加载中...