1.超时的写法(没有用堆优化)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define inf 0x7fffffff
void solve();
const int N = 2e5 + 10;
ll n,m,k,u,v,w,f;
signed main() {
IOS;
ll t;
// cin >> t;
t = 1;
while(t--)
solve();
return 0;
}
vector<ll> djst1(N,inf),djst2(N,inf);
struct eage{ll v,w,ne;};
vector<eage> e1(N),e2(N);
ll idx1,idx2;
vector<ll> h1(N,-1),h2(N,-1);
vector<bool> vis1(N,0),vis2(N,0);
void add1(ll u,ll v,ll w)
{
// e1[idx1] = {v,w,idx1};**********************mdmdmdmdmdmdmdmd*************
e1[idx1] = {v,w,h1[u]};
h1[u] = idx1 ++;
}
void add2(ll u,ll v,ll w)
{
// e2[idx2] = {v,w,idx2};**************************mdmdmdm************
e2[idx2] = {v,w,h2[u]};
h2[u] = idx2 ++;
}
struct place{ll u,v;};
void solve() {
cin >> n >> m >> k;
djst1[1] = 0;
map<place,PII> mp;
for(int i = 1; i <= m; ++ i)
{
cin >> u >> v >> w >> f;
if(f)
{
add1(u,v,w);
add1(v,u,w);
}
add2(u,v,w);
add2(v,u,w);
}
// 1 - n dij
priority_queue<ll> q;
q.push(1);
while(!q.empty())
{
ll t = q.top();
vis1[t] = 1;
q.pop();
for(int i = h1[t]; ~ i; i = e1[i].ne)
{
ll tv = e1[i].v,tw = e1[i].w;
djst1[tv] = min(djst1[tv],djst1[t]+tw);
if(vis1[tv]) continue;
q.push(tv);
}
}
while(!q.empty()) q.pop();
q.push(k);
djst2[k] = 0;
while(!q.empty())
{
ll t = q.top();
vis2[t] = 1;
q.pop();
for(int i = h2[t]; ~ i; i = e2[i].ne)
{
ll tv = e2[i].v,tw = e2[i].w;
djst2[tv] = min(djst2[tv],djst2[t]+tw);
if(vis2[tv]) continue;
q.push(tv);
}
}
if(djst2[n] == inf && djst1[n] == inf)
{
cout << -1 << endl;
}
else
cout << min(djst1[n],djst1[k]+djst2[n]) << endl;
}
2.内存超限
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<ll,ll> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
// #define inf 1'000'000'000'000'000
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve();
const int N = 2e5 + 10;
ll n,m,k,u,v,w,f;
signed main() {
IOS;
ll t;
// cin >> t;
t = 1;
while(t--)
solve();
return 0;
}
vector<ll> djst1(N,inf),djst2(N,inf);
struct eage{int v,w,ne;};
vector<eage> e1(N),e2(N);
ll idx1,idx2;
vector<int> h1(N,-1),h2(N,-1);
vector<bool> vis1(N,0),vis2(N,0);
void add1(ll u,ll v,ll w)
{
// e1[idx1] = {v,w,idx1};**********************mdmdmdmdmdmdmdmd*************
e1[idx1] = {(int)v,(int)w,h1[u]};
h1[u] = idx1 ++;
}
void add2(ll u,ll v,ll w)
{
// e2[idx2] = {v,w,idx2};**************************mdmdmdm************
e2[idx2] = {(int)v,(int)w,h2[u]};
h2[u] = idx2 ++;
}
void solve() {
cin >> n >> m >> k;
djst1[1] = 0;
for(int i = 1; i <= m; ++ i)
{
cin >> u >> v >> w >> f;
if(f)
{
add1(u,v,w);
add1(v,u,w);
}
add2(u,v,w);
add2(v,u,w);
}
// 1 - n dij
priority_queue<pii,vector<pii>,greater<pii>> q;
q.push({0,1});
while(!q.empty())
{
ll u = q.top().y;
q.pop();
if(vis1[u]) continue;
vis1[u] = 1;
for(int i = h1[u]; ~ i; i = e1[i].ne)
{
ll v = e1[i].v, w = e1[i].w;
q.push({djst1[v],v});
djst1[v] = min(djst1[v],djst1[u]+w);
}
}
q.push({0,k});
djst2[k] = 0;
while(!q.empty())
{
ll u = q.top().y;
q.pop();
if(vis2[u]) continue;
vis2[u] = 1;
for(int i = h2[u]; ~ i; i = e2[i].ne)
{
ll v = e2[i].v, w = e2[i].w;
q.push({djst2[v],v});
djst2[v] = min(djst2[v],djst2[u]+w);
}
}
if(djst1[n] == inf && (djst1[k] == inf || djst2[n] == inf))//直接到不了
{
cout << -1 << endl;
}
else
cout << min(djst1[n],djst1[k] + djst2[n]) << endl;
}
//4568
AC代码
主要修改如下:N = 4 e 5
先进行比较,在进行push
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<ll,ll> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
// #define inf 1'000'000'000'000'000
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve();
const int N = 4e5 + 10;
ll n,m,k,u,v,w,f;
signed main() {
IOS;
ll t;
// cin >> t;
t = 1;
while(t--)
solve();
return 0;
}
vector<ll> djst1(N,inf),djst2(N,inf);
struct eage{ll v,w,ne;};
vector<eage> e1(N),e2(N);
ll idx1,idx2;
vector<ll> h1(N,-1),h2(N,-1);
vector<bool> vis1(N,0),vis2(N,0);
void add1(ll u,ll v,ll w)
{
// e1[idx1] = {v,w,idx1};**********************mdmdmdmdmdmdmdmd*************
e1[idx1] = {v,w,h1[u]};
h1[u] = idx1 ++;
}
void add2(ll u,ll v,ll w)
{
// e2[idx2] = {v,w,idx2};**************************mdmdmdm************
e2[idx2] = {v,w,h2[u]};
h2[u] = idx2 ++;
}
void solve() {
cin >> n >> m >> k;
djst1[1] = 0;
for(int i = 1; i <= m; ++ i)
{
cin >> u >> v >> w >> f;
if(f)
{
add1(u,v,w);
add1(v,u,w);
}
add2(u,v,w);
add2(v,u,w);
}
// 1 - n dij
priority_queue<pii,vector<pii>,greater<pii>> q;
q.push({0,1});
while(!q.empty())
{
ll u = q.top().y;
q.pop();
if(vis1[u]) continue;
vis1[u] = 1;
for(int i = h1[u]; ~ i; i = e1[i].ne)
{
ll v = e1[i].v, w = e1[i].w;
djst1[v] = min(djst1[v],djst1[u]+w); q.push({djst1[v],v});
}
}
q.push({0,k});
djst2[k] = 0;
while(!q.empty())
{
ll u = q.top().y;
q.pop();
if(vis2[u]) continue;
vis2[u] = 1;
for(int i = h2[u]; ~ i; i = e2[i].ne)
{
ll v = e2[i].v, w = e2[i].w;
djst2[v] = min(djst2[v],djst2[u]+w);
q.push({djst2[v],v});
}
}
if(djst1[n] == inf && (djst1[k] == inf || djst2[n] == inf))//直接到不了
{
cout << -1 << endl;
}
else
cout << min(djst1[n],djst1[k] + djst2[n]) << endl;
}
//4568