1ios
spfa模版測試
#include<map> #include<stack> #include<queue> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #define inf 0x3f3f3f3f #define maxn 100005 #define mem(a,b) memset(a,b,sizeof(a)) #define ll long long using namespace std; int n,m,src; vector<pair<int,int> >g[maxn+10]; int dist[maxn+10]; bool inque[maxn+10]; queue<int>que; void spfa(){ mem(dist,inf); mem(inque,false); dist[src]=0; while(!que.empty())que.pop(); que.push(src); inque[src]=true; while(!que.empty()){ int u=que.front(); que.pop(); for(int i=0;i<g[u].size();i++){ if(dist[u]+g[u][i].second<dist[g[u][i].first]){ dist[g[u][i].first]=dist[u]+g[u][i].second; if(!inque[g[u][i].first]){ inque[g[u][i].first]=true; que.push(g[u][i].first); } } } inque[u]=false; } } int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=0;i<=maxn;i++){g[i].clear();} src=0; for(int i=0;i<m;i++){ int a,b,c;scanf("%d%d%d",&a,&b,&c); g[a].push_back(make_pair(b,c)); } int s;scanf("%d",&s); for(int i=0;i<s;i++){ int x;scanf("%d",&x); g[0].push_back(make_pair(x,0)); } int t;scanf("%d",&t); for(int i=0;i<t;i++){ int x;scanf("%d",&x); g[x].push_back(make_pair(n+1,0)); } spfa(); printf("%d\n",dist[n+1]); } }