话不多说上代码
链式前向星233
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1000,_max=0x3fffffff;
//链式前向星 4 struct bian{ 5 int from,to,dist; 6 bian(int u,int v,int d) : from(u),to(v),dist(d){} 7 };//定义一个 边 类型 8 struct heapn{ 9 int u,d; 10 heapn(int u,int d): u(u),d(d) {} 11 bool operator <(const heapn & r) const { 12 return d>r.d; 13 } 14 };//定义一个堆 15 struct Di{ //重点: 16 int n,m; 17 vector<int> g[maxn]; 18 vector<bian> bi; 19 int dis[maxn],pre[maxn],v[maxn]; 20 void jiaru(int u ,int v ,int d) { 21 bi.push_back(bian(u,v,d)); 22 g[u].push_back(bi.size()-1); 23 } 24 void ini(int v_size){ 25 n=v_size; 26 bi.clear(); 27 for(int i=0;i<n;i++) g[i].clear(); 28 memset(pre ,0 ,sizeof(pre)); 29 } 30 void di(int s){ 31 for(int i=0;i<n;i++) dis[i]=_max; 32 dis[s]=0; 33 memset(v,0,sizeof(v)); 34 priority_queue<heapn> q; 35 q.push(heapn(s,0)); 36 while(!q.empty()){ 37 heapn t=q.top(); 38 q.pop(); 39 int x=t.u; 40 if(v[x]) continue; 41 v[x]=1; 42 for(int j=0;j<n;j++){ 43 bian & e =bi[g[x][j]]; 44 if(dis[x]+e.dist<dis[e.to]){ 45 dis[e.to]=dis[x]+e.dist; 46 pre[e.to]=x; 47 q.push(heapn(e.to,dis[e.to])); 48 } 49 } 50 } 51 } 52 }; 53 int main(){ 54 Di a; 55 int u,v,d; 56 a.ini(10); 57 while(1){ 58 cin>>u>>v>>d; 59 a.jiaru(u,v,d); 61 } 62 a.di(1); 63 cout<<a.dis[5]<<endl; 64 return 0; 65 }