浙大PAT (Advanced Level) Practise 1003. Emergency (25)
2013-06-14 13:57
447 查看
/*本题是类似于最短路径的一题,只是加了更多变量,采用dijkstra算法的变形即可解题,代码如下:*/ #include<iostream> using namespace std; #define inf 100000000 int n,m,s,e; int paths[505]; int teams[505]; int maxteams[505]; int dist[505]; int used[505]; int mat[505][505]; void dij() //dijkstra算法的变式 { dist[s]=0; paths[s]=1; maxteams[s]=teams[s]; for(int i=0;i<n;++i) { int p=0,min=inf; for(int j=0;j<n;++j) //选出距离最近点 { if(used[j]==0&&dist[j]<min) { min=dist[j]; p=j; } } if(p==e||min==inf) return; used[p]=1; for(int j=0;j<n;++j) //更新相关信息 { if(used[j]==1) continue; //j点已经被并入最短路径集合时,跳过 int distcost=dist[p]+mat[p][j]; if(dist[j]>distcost) //新的路径比原来的短,更新最短路径值等 { dist[j]=distcost; paths[j]=paths[p]; maxteams[j]=maxteams[p]+teams[j]; } else if(dist[j]==distcost) { paths[j]=paths[p]+paths[j]; if(maxteams[p]+teams[j]>maxteams[j])//新路径与原来路径长度相同时,如果经过该条路径召集的队伍数较多时,更新之 maxteams[j]=maxteams[p]+teams[j]; } } } } int main() { while(cin>>n>>m>>s>>e) { for(int i=0;i<n;++i) //初始化各种数据 { paths[i]=0; teams[i]=0; dist[i]=inf; used[i]=0; for(int j=0;j<n;++j) mat[i][j]=mat[j][i]=inf; } for(int i=0;i<n;++i) cin>>teams[i]; for(int i=0;i<m;++i) { int a,b; cin>>a>>b; cin>>mat[a][b]; mat[b][a]=mat[a][b]; } dij(); //调用dij()函数求解 cout<<paths[e]<<' '<<maxteams[e]<<endl; } return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1003. Emergency (25) Dijstra扩展应用
- 浙大PAT (Advanced Level) Practise 1002. A+B for Polynomials (25)
- 浙大PAT (Advanced Level) Practise 1009 Product of Polynomials (25)
- 浙大PAT (Advanced Level) Practise 1043 Is It a Binary Search Tree (25)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- 浙大PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- 1003. Emergency (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1003. Emergency (25)
- 浙大PAT (Advanced Level) Practise 1012 The Best Rank (25)
- PAT (Advanced Level) Practise - 1003. Emergency (25)
- PAT (Advanced Level) Practise 1102. Invert a Binary Tree (25) 二叉树的层序和中序遍历
- 浙大PAT (Advanced Level) Practise 1007
- PAT (Advanced Level) Practise 1016. Phone Bills (25)
- PAT (Advanced Level) Practise - 1007. Maximum Subsequence Sum (25)
- 1024. Palindromic Number (25)【简易大数加法+回文】——PAT (Advanced Level) Practise
- 1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise
- 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1060 Are They Equal (25)
- PAT (Advanced Level) Practise 1125 Chain the Ropes (25)
- PAT (Advanced Level) Practise 1017 Queueing at Bank (25)