PAT 1018. Public Bike Management
2014-04-10 15:16
435 查看
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.
View Code
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.
// 1018pat2.cpp : 定义控制台应用程序的入口点。 // #include <iostream> #include <map> #include <vector> #include <math.h> using namespace std; const int INF=0x7fffffff; const int N=501; struct Edge { int next; int dis; int cost; }; struct Edges { vector<Edge> list; }edges ; map<int,int> cmap; int dist ; int cost ; int parent ; bool mark ; int cmax,n,sp,M; void print_path(int x) { if(x==0) cout<<x<<"->"; else { print_path(parent[x]); if(x==sp) cout<<x; else cout<<x<<"->"; } } int main() { while(cin>>cmax>>n>>sp>>M) { for(int i=0;i<=n;++i) { mark[i]=false; dist[i]=INF; cost[i]=0; parent[i]=-1; edges[i].list.clear(); } cmap[0]=0; int tmp; for(int i=1;i<=n;++i) { cin>>tmp; cmap[i]=tmp; } Edge edge; int a,b,dis; for(int i=1;i<=M;++i) { cin>>a>>b>>dis; edge.dis=dis; edge.cost=cmap[b]; edge.next=b; edges[a].list.push_back(edge); edge.cost=cmap[a]; edge.next=a; edges[b].list.push_back(edge); } mark[0]=true; dist[0]=0; cost[0]=0; int newP=0; int next; int costs; int d; for(int i=1;i<=n;++i) { if(newP==2) newP=newP; for(int j=0;j<edges[newP].list.size();++j) { next=edges[newP].list[j].next; if(mark[next]) continue; costs=edges[newP].list[j].cost; d=edges[newP].list[j].dis; if(dist[next]==-1||(dist[next]>dist[newP]+d)||(dist[next]==dist[newP]+d&&abs(cost[next])>abs(cost[newP]+costs-cmax/2))) { dist[next]=dist[newP]+d; cost[next]=cost[newP]+costs-cmax/2; parent[next]=newP; } } int min=INF; for(int i=1;i<=n;++i) { if(mark[i]||dist[i]==-1) continue; if(dist[i]<min) { min=dist[i]; newP=i; } } mark[newP]=true; } int res=cost[sp]; if(res<0) { cout<<abs(res)<<" "; print_path(sp); cout<<" "<<0<<endl; } else { cout<<0<<" "; print_path(sp); cout<<" "<<res<<endl; } } return 0; }
View Code
相关文章推荐
- 【PAT1018】 Public Bike Management 单源最短路径&路径记录回溯
- Pat(Advanced Level)Practice--1018(Public Bike Management)
- PAT 1018. Public Bike Management
- pat 1018. Public Bike Management (dijkstra+dfs)
- 【PAT】甲级1018 - Public Bike Management(最短路 & dfs & 递归 & 好题)
- PAT甲级1018【Public Bike Management】 dfs
- PAT-Java-1018-Public Bike Management (30)
- 【PAT1018】Public Bike Management
- PAT 1018 Public Bike Management(Dijkstra 最短路)
- 1018. Public Bike Management
- PAT 1018 Public Bike Management(Dijkstra 最短路)
- 1018. Public Bike Management
- 1018. Public Bike Management 解析
- PAT (Advanced Level)1018. Public Bike Management (30) 迪杰斯特拉算法 DFS 递归(难)
- PAT1018 Public Bike Management
- PAT(甲级)1018. Public Bike Management (30)
- 1018. Public Bike Management
- 1018. Public Bike Management (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1018 Public Bike Management (30)
- PAT-1018 Public Bike Management(dijkstra + dfs)