sol 1:S建立反向图
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 100, INF = 0x3f3f3f3f;
int a[N], ne[N], h[N], e[N], dis[N], idx;
bool st[N];
int w[N],n,m,s;
typedef pair<int, int> PII;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dij(int s)
{
memset(dis, 0x3f, sizeof dis);
memset(st, 0, sizeof st);
dis[s] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, s});
while (!heap.empty())
{
auto t = heap.top();
heap.pop();
int ver = t.second,distance=t.first;
if (st[ver])
continue;
st[ver] = 1;
for (int i = h[ver]; ~i; i = ne[i])
{
int j=e[i];
if (dis[j] > distance + w[i])
{
dis[j] = distance + w[i];
heap.push({dis[j], j});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n >> m >> s)
{
idx=0;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
add(b, a, c);
}
int k;
cin >> k;
for (int i = 1; i <= k; i++)
cin >> a[i];
dij(s);
int res = INF;
for (int i = 1; i <= k; i++)
res = min(res, dis[a[i]]);
if (res==INF) cout<<"-1\n";
else cout<<res<<endl;
}
return 0;
}
sol 2:建立虚拟源点(dij)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100, INF = 0x3f3f3f3f;
int a[N], ne[N], h[N], e[N], dis[N], idx;
bool st[N];
int w[N],n,m,s;
typedef pair<int, int> PII;
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dij()
{
memset(st,0,sizeof st);
memset(dis,0x3f,sizeof dis);
priority_queue<PII,vector<PII>,greater<PII> > heap;
heap.push({0,0});
dis[0]=0;
while(!heap.empty())
{
auto t=heap.top();
heap.pop();
int ver=t.second,distance=t.first;
for(int i=h[ver];~i;i=ne[i])
{
int j=e[i];
if (dis[j]>distance+w[i])
{
dis[j]=distance + w[i];
heap.push({dis[j],j});
}
}
}
}
int main(){
ios::sync_with_stdio(false);
while (cin >> n >> m >> s)
{
idx=0;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int k;
cin >> k;
for (int i = 1; i <= k; i++)
{
int b;
cin>>b;
add(0,b,0);
}
dij();
int res = dis[s];
if (res==INF) cout<<"-1\n";
else cout<<res<<endl;
}
return 0;
}
sol 3虚拟源点+spfa
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100, INF = 0x3f3f3f3f;
int a[N], ne[N], h[N], e[N], dis[N], idx;
bool st[N];
int w[N],n,m,s;
typedef pair<int, int> PII;
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void spfa()
{
memset(st,0,sizeof st);
memset(dis,0x3f,sizeof dis);
queue<int> q;
q.push(0);
dis[0]=0;
while(!q.empty())
{
auto t=q.front();
q.pop();
st[t]=0;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if (dis[j]>dis[t]+w[i])
{
dis[j]=dis[t] + w[i];
if (!st[j])
{
q.push(j);
st[j]=1;
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
while (cin >> n >> m >> s)
{
idx=0;
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int k;
cin >> k;
for (int i = 1; i <= k; i++)
{
int b;
cin>>b;
add(0,b,0);
}
spfa();
int res = dis[s];
if (res==INF) cout<<"-1\n";
else cout<<res<<endl;
}
return 0;
}