非传统起点终点的最短路问题
#include<bits/stdc++.h>
using namespace std;
const int N = 2510,M = 6200 * 6200 + 10;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
bool st[N];
int n,m,s,t;
queue<int> q;
void add(int a,int b,int c)
{
e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
void spfa()
{
memset(dist,0x3f,sizeof dist);
dist[s] = 0;
q.push(s);
st[s] = true;
while(q.size())
{
int p = q.front();
q.pop();
st[p] = false;
for(int i = h[p];~i;i = ne[i])
{
int j = e[i];
if(dist[j] > dist[p] + w[i])
{
dist[j] = dist[p] + w[i];
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m>>s>>t;
for(int i = 0;i<m;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
spfa();
cout<<dist[t]<<endl;
return 0;
}