是数据水还是这样做本来就没有问题?
查看原帖
是数据水还是这样做本来就没有问题?
166209
clever_sheep楼主2019/10/30 16:03

路障

这题是求严格次短路的模板,我交了个看似错误的代码也A了:记录1

你们看一下第55行那句删掉的代码有用吗?我感觉删掉会错啊,是数据水吗?

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define QWQ cout<<"QwQ"<<endl;
#define ll long long
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
const int N=501010;
const int qwq=303030;
const int inf=0x3f3f3f3f;
int n,m,k;
int belong[N];
bool cut[N];
bool vis[N],vis2[N];
int ru[N],dis[N],low[N];
struct E{ int to,we; };
struct D{ int id,d; };
inline bool cmp (E aa,E bb) { return aa.we > bb.we; }
inline bool operator < (D aa,D bb) { return aa.d > bb.d; }
vector <E> e[N];
priority_queue <D> q;

inline int read() {
	int sum = 0, f = 1; char c = getchar();
	while(c<'0' || c>'9') { if(c=='-') f = -1; c = getchar(); }
	while(c>='0'&&c<='9') { sum = sum * 10 + c - '0'; c = getchar(); }
	return sum * f;
}

void DIJ() {
	memset(dis,0x3f,sizeof(dis));
	memset(low,0x3f,sizeof(low));
	dis[1] = 0; q.push((D){1,0});
	while(!q.empty()) {
		D now = q.top(); q.pop();
		int u = now.id;
		if(now.d != dis[u] && now.d != low[u]) continue;
		for(int i=0;i<e[u].size();i++) {
			int v = e[u][i].to;
			int w = e[u][i].we;
			if(cut[v]) continue;
			if(low[u]!=inf) {
				if(low[v] > low[u] + w) {
					low[v] = low[u] + w;
					q.push((D){v,low[v]});
				}
			}
			if(dis[u]!=inf) {
				if(dis[v] > dis[u] + w) {
//					low[v] = dis[v];     //最短路更新的话,次短路肯定也得更新啊。必须加这句话吧?
					dis[v] = dis[u] + w;
					q.push((D){v,dis[v]});
				}
				else if(low[v] > dis[u] + w && dis[u] + w > dis[v]) {
					low[v] = dis[u] + w;
					q.push((D){v,low[v]});
				}
			}
		}
	}
}

int main() {
	int x,y,z;
	n = read(); m = read();
	while(m--) {
		x = read(); y = read(); z = read();
		e[x].push_back((E){y,z});
		e[y].push_back((E){x,z});
	}
	DIJ();
	if(low[n]==inf) cout<<"-1";
	else cout<<low[n];
	return 0;
}

谢谢大佬们!orz

2019/10/30 16:03
加载中...