pat 1018. Public Bike Management (dijkstra+dfs)
最短路問題,複習dijkstra和dfs
首先dijkstra求出最短路,然後dfs求出所有路徑
求出路徑中send最小的,再求其中back最小的
point:題設的要求是修正最短路徑上的所有點,實際上必須嚴格遵循訪問順序修改每個點的權值(eg:要依次修改權值分別為3,10的兩個點,必須send2back5而非簡單地back3),亦即到達目標點sp後的回程是不能修改的。
如果錯誤地理解為簡單地將最短路徑上的所有點上的權值全部改為half-full的話,會有兩個case過不了
#include<stdio.h> #include<iostream> #include<stack> #include<map> #include<string> #include<algorithm> using namespace std; int cmax,n,sp,m,longest; const int MAX=0x7fffffff; const int number=510; int in[number]; int ld[number][number]; int dis[number]; int bl[number]; int pre[number]; int change[number]; stack<int> sta; multimap<int,string> mp; multimap<int,string> mp2; multimap<int,string>::iterator mi; int used[number]; int mm=MAX; int minsum=MAX; string itos(int num) { string s=""; if(num<10) { s=" "; s[0]=(num+'0'); } if(num>=10&&num<=99) { s=" "; s[0]=(num%10+'0'); s[1]=(num/10+'0'); } if(num>=100) { s=" "; s[0]=(num%10+'0'); s[1]=((num/10)%10+'0'); s[2]=(num/100+'0'); } return s; } void dijkstra(int start) { int min,i,u,j; bl[start]=1; for(i=1; i<=n; ++i) { dis[i]=ld[start][i]; if(dis[i] == MAX) pre[i] = -1; else pre[i] = start; } for(i=1; i<=n; i++) { min=MAX; u=start; for(j=1;j<=n;j++) if(dis[j]<min&&!bl[j]) { min=dis[j]; u=j; } bl[u]=1; for(j=1;j<=n;j++) if(ld[u][j]<MAX&&!bl[j]) { if(dis[u]+ld[u][j]<dis[j]) { dis[j]=dis[u]+ld[u][j]; pre[j]=u; } } } } void display() { stack<int> stmp=sta,stmp2; int x=0,sum=0,store=0; string str=""; while(!stmp.empty()) { x=stmp.top(); stmp.pop(); if(x==0) break; str+=itos(x); str+=">-"; stmp2.push(change[x]); } while(!stmp2.empty()) { x=stmp2.top(); stmp2.pop(); if(x>=0) { store+=x; } else { if(-x<=store) { store+=x; } else { sum+=(-x-store); store=0; } } } str+="0"; reverse(str.begin(),str.end()); if(sum>0) { if(sum==minsum) { mp.insert(make_pair(store,str)); } if(sum<minsum) { mp.clear(); mp.insert(make_pair(store,str)); minsum=sum; } } else mp2.insert(make_pair(store,str)); } void dfs(int start,int l) { int rd=0,i,u; sta.push(start); if(l>longest) { sta.pop(); return; } if(start==sp&&l==longest) { mm=l; display(); sta.pop(); return; } u=start; for(i=1;i<=n;i++) { if(ld[u][i]<=longest&&!used[i]&&u!=i) { used[i]=1; dfs(i,l+ld[u][i]); used[i]=0; } } sta.pop(); } int main() { int i,j,s1,s2,t; scanf("%d %d %d %d",&cmax,&n,&sp,&m); for(i=0;i<=n;i++) for(j=0;j<=n;j++) ld[i][j]=MAX; ld[0][0]=0; for(i=1;i<=n;i++) { scanf("%d",&in[i]); ld[i][i]=0; } for(i=0;i<m;i++) { scanf("%d %d %d",&s1,&s2,&t); ld[s1][s2]=t; ld[s2][s1]=t; } for(i=1;i<=n;i++) { change[i]=in[i]-(cmax/2); } dijkstra(0); longest=dis[sp]; dfs(0,0); if(!mp2.empty()) cout<<"0 "<<mp2.begin()->second<<" "<<mp2.begin()->first<<endl; else cout<<minsum<<" "<<mp.begin()->second<<" "<<mp.begin()->first<<endl; return 0; }
相關推薦
pat 1018. Public Bike Management (dijkstra+dfs)
最短路問題,複習dijkstra和dfs 首先dijkstra求出最短路,然後dfs求出所有路徑 求出路徑中send最小的,再求其中back最小的 point:題設的要求是修正最短路徑上的所有點,實際上必須嚴格遵循訪問順序修改每個點的權值(eg:要依次修改權值分別為3,1
PAT 1018 Public Bike Management(Dijkstra 最短路)
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 stat
PAT 1018 Public Bike Management (30 分)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all ove
1018 Public Bike Management (30 分)(Dij+DFS)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over th
【未完成】【笨方法學PAT】1018 Public Bike Management (30 分)
一、題目 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
1018 Public Bike Management (30 分)
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
1018 Public Bike Management (30 分)(圖的遍歷and最短路徑)
這題不能直接在Dijkstra中寫這個第一 標尺和第二標尺的要求 因為這是需要完整路徑以後才能計算的 所以寫完後可以在遍歷 #include<bits/stdc++.h> using namespace std; int c
PATA 1018 Public Bike Management(30 分)解題報告
1018 Public Bike Management(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from a
1018 Public Bike Management (30) Dijkstra算法 + DFS
== net pub blog sta 認識 pop false inf 題目及題解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪傑斯特拉重新認識 兩個核心的存儲結構: int dis[n]
PAT 1018 Public Bike Management
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 b
【ACM】- PAT. 1018 Public Bike Management 【圖
題目連結 題目分析 給出結點資訊,輸出最短路徑; 總站編號為0,其他站點編號為1-N 多條最短路徑時,其他標尺: 在最短路徑過程中,必須把每個結點的權值調整到題目要求的最佳; 標尺一:選擇需要從總站帶出最少量的路徑 標尺二:仍有多條,則選擇需要
最短路_1018 Public Bike Management (30 分)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from a
PAT Advanced Level 1018. Public Bike Management (30)(Java and C++)
PAT Advanced Level 1018. Public Bike Management (30) 最短路徑問題。題意有三:1.時間最短 2.送出車輛最少 3.回收車輛最少 ps:(注意我的lessThan函式) 我的思路:是 SPFA(求出最短路徑)
PAT甲級1018 Public Bike Management (30)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from
PAT甲級1018. Public Bike Management (30)
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may r
1018. Public Bike Management (30)-PAT
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 stat
1018 Public Bike Management (30)(30 分)(C++)
1018 Public Bike Management (30)(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists f
1018 Public Bike Management (30)
const ios perf orm repr sts div break tput There is a public bike service in Hangzhou City which provides great convenience to the touris
PAT-ADVANCED1018——Public Bike Management
我的PAT-ADVANCED程式碼倉:https://github.com/617076674/PAT-ADVANCED 原題連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024
1018 Public Bike Management
ret content test describe format ostream require lse oos There is a public bike service in Hangzhou City which provides great convenience