标签:namespace div class continue out [] 结果 cst 最短路
第一次写次短路
从起点和终点出发分别跑两次单源最短路得到dis1[] 和 dis2[],对所有边e的起点st,终点ed,将备选方案dis1[st]+dis2[ed]+e.length加入ans中,ans排序,选取次小的结果就是答案
没看数据范围错了两次,下次注意……
#include<queue> #include<cstdio> #include<vector> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef long long LL; vector<pair<LL,LL> > v[100100]; int vis[100100]; int st,ed,ind,test,n,m; LL dis,len; pair<LL,LL> tmp; LL dis1[100100],dis2[100100]; priority_queue<pair<LL,LL>,vector<pair<LL,LL> >,greater<pair<LL,LL> > > q; void dij(int st,LL *arr){ while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); for(int i = 1;i<=n;i++) arr[i] = 1e17; arr[st] = 0; q.push(make_pair(0,st)); while(!q.empty()){ tmp = q.top(); q.pop(); ind = tmp.second; if(vis[ind]) continue; vis[ind] = 1; arr[ind] = dis = tmp.first; for(int i = 0;i<v[ind].size();i++){ int to = v[ind][i].second; if(!vis[to]){ if(arr[to]>arr[ind]+v[ind][i].first) arr[to] = arr[ind]+v[ind][i].first,q.push(make_pair(arr[to],to)); } } } } int main(){ scanf("%d",&test); while(test--){ scanf("%d%d",&n,&m); for(int i = 1;i<=n;i++) v[i].clear(); for(int i = 0;i<m;i++){ scanf("%d%d%lld",&st,&ed,&len); v[st].push_back(make_pair(len,ed)); v[ed].push_back(make_pair(len,st)); } dij(1,dis1); dij(n,dis2); //for(int i = 1;i<=n;i++) cout<<"dis1: "<<i<<" "<<dis1[i]<<endl; //for(int i = 1;i<=n;i++) cout<<"dis2: "<<i<<" "<<dis2[i]<<endl; vector<LL> ans; for(int i = 1;i<=n;i++){ for(int j = 0;j<v[i].size();j++){ int to = v[i][j].second; ans.push_back(v[i][j].first+dis1[i]+dis2[to]); } } sort(ans.begin(),ans.end()); LL mmin = ans[0]; for(int i = 0;i<ans.size();i++){ if(ans[i] != mmin){ cout<<ans[i]<<endl; break; } } } return 0; }
标签:namespace div class continue out [] 结果 cst 最短路
原文地址:http://www.cnblogs.com/Invisible-full-moon/p/7442342.html