跑dijiestra每个点的最短路径
#include<iostream> #include<Vector> #include<cstring> #include<queue> #include<cstdio> #define mp make_pair using namespace std; typedef long long ll; typedef pair<ll,ll> PII; const ll inf=(ll)(1e15); priority_queue<PII,vector<PII>,greater<PII> >q; ll n,m,x,y,u,v,w,tar,t,c,val; ll dis[1010][1010]; ll used[1010],d[1010]; vector<PII>graph[1010]; void dijiestra1(int a) { for(int i=1;i<=n;i++) d[i]=inf; memset(used,0,sizeof(used)); d[a]=0; q.push(mp(0,a)); while(!q.empty()) { PII x=q.top();q.pop(); int u=x.second; if(used[u]) continue; used[u]=1; for(int i=0;i<graph[u].size();i++) { x=graph[u][i]; int v=x.first,val=x.second; if(d[v]>d[u]+val){d[v]=d[u]+val;q.push(mp(d[v],v));} } } } int main() { cin>>n>>m; cin>>x>>y; for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&val); graph[u].push_back(mp(v,val)); graph[v].push_back(mp(u,val)); } for(int i=1;i<=n;i++) { dijiestra1(i); for(int j=1;j<=n;j++) dis[i][j]=dis[j][i]=d[j]; } for(int i=1;i<=n;i++) graph[i].clear(); for(int i=1;i<=n;i++) { scanf("%d%d",&t,&c); for(int j=1;j<=n;j++) if(dis[i][j]<=t) graph[i].push_back(mp(j,c)); } dijiestra1(x); if(d[y]==inf) d[y]=-1; cout<<d[y]<<endl; return 0; }