标签:travel view space hint mil nes ace printf navig
个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book【1】=1就错了.....
纠结中....
Input
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iomanip> 6 #include<algorithm> 7 using namespace std; 8 #define inf 1<<29 9 int t,n; 10 int cow[1005][1005]; 11 int dis[1005]; 12 int book[1005]; 13 void dijkstra() 14 { 15 memset(book,0,sizeof(book)); 16 for(int i=1;i<=n;i++) dis[i]=cow[1][i]; 17 dis[1]=0; 18 book[1]=1; 19 for(int i=1;i<=n;i++) 20 { 21 int mina=inf; 22 int k; 23 for(int j=1;j<=n;j++) 24 { 25 if(book[j]==0&&mina>dis[j]) 26 { 27 mina=dis[j]; 28 k=j; 29 } 30 } 31 book[k]=1; 32 for(int j=1;j<=n;j++) 33 { 34 if(book[j]==0&&dis[j]>dis[k]+cow[k][j]) 35 dis[j]=dis[k]+cow[k][j]; 36 } 37 } 38 } 39 int main() 40 { 41 while(cin>>t>>n){ 42 int x,y,z; 43 for(int i=1;i<=n;i++) 44 for(int j=1;j<=n;j++) 45 if(i==j) cow[i][j]=0; 46 else cow[i][j]=inf; 47 for(int i=1;i<=t;i++){ 48 cin>>x>>y>>z; 49 if(cow[x][y]>z) 50 cow[x][y]=cow[y][x]=z; 51 52 } 53 dijkstra(); 54 cout<<dis[n]<<endl;} 55 return 0; 56 }
Bellman-Ford算法
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iomanip> 6 #include<algorithm> 7 using namespace std; 8 #define inf 1<<29 9 int t,n; 10 int u[2005],v[2005],w[2005]; 11 int dis[1005]; 12 int book[1005]; 13 int main() 14 { 15 while(cin>>t>>n) 16 { 17 for(int i=1;i<=t;i++) 18 cin>>u[i]>>v[i]>>w[i]; 19 for(int i=1;i<=n;i++) 20 dis[i]=inf; 21 dis[1]=0; 22 for(int i=1;i<=n;i++) 23 for(int j=1;j<=t;j++){ 24 if(dis[v[j]]>dis[u[j]]+w[j]) 25 dis[v[j]]=dis[u[j]]+w[j]; 26 if(dis[u[j]]>dis[v[j]]+w[j]) 27 dis[u[j]]=dis[v[j]]+w[j]; 28 29 } 30 cout<<dis[n]<<endl; 31 32 } 33 return 0; 34 }
Queue
1 /* 2 spfa 3 Memory 256K 4 Time 32MS 5 */ 6 #include <iostream> 7 #include <queue> 8 using namespace std; 9 #define inf 1<<29 10 #define MAXM 4005 11 #define MAXV 1005 12 13 typedef struct{ 14 int a,b,w,next; 15 }Edge; 16 17 Edge edge[MAXM]; 18 int n,m,headlist[MAXV]; 19 20 void spfa(){ 21 int i,d[MAXV],v,vis[MAXV]; 22 queue <int>q; 23 for(i=2;i<=n;i++){ 24 d[i]=inf; 25 vis[i]=0; 26 } 27 d[1]=0; 28 vis[1]=1; 29 q.push(1); 30 while(!q.empty()){ 31 v=q.front();q.pop(); 32 vis[v]=0; 33 34 for(i=headlist[v];i!=-1;i=edge[i].next) 35 if(d[v]+edge[i].w<d[edge[i].b]){ 36 d[edge[i].b]=d[v]+edge[i].w; 37 if(!vis[edge[i].b]){ 38 vis[edge[i].b]=1; 39 q.push(edge[i].b); 40 } 41 } 42 } 43 printf("%d\n",d[n]); 44 } 45 46 int main(){ 47 int i,a,b,c; 48 while(~scanf("%d%d",&m,&n)){ 49 for(i=1;i<=n;i++) headlist[i]=-1; 50 for(i=1;i<=2*m;i+=2){ 51 scanf("%d%d%d",&a,&b,&c); 52 edge[i].a=a; 53 edge[i].b=b; 54 edge[i].w=c; 55 edge[i].next=headlist[a]; 56 headlist[a]=i; 57 edge[i+1].a=b; 58 edge[i+1].b=a; 59 edge[i+1].w=c; 60 edge[i+1].next=headlist[b]; 61 headlist[b]=i+1; 62 } 63 spfa(); 64 } 65 return 0; 66 }
Til the Cows Come Home (最短路模板题)
标签:travel view space hint mil nes ace printf navig
原文地址:http://www.cnblogs.com/blvt/p/7307800.html