076

DS HW4

076
May 18th, 2024 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. typedef struct del{
  5.     int position,totalpath,id;
  6.     vector<int> path;
  7. }del;
  8. map<int,del> mission,drop;
  9. vector<int> traffic(101);
  10. vector<vector<int>> path2(101);
  11. int gp[100][100],v,e,n;
  12. //gp[x][y],x<y表xy距離,x>y表xy空間,x=y表點x外送員數量,v vertex,e edge,n deliver
  13. struct cmp{
  14.     bool operator()(const del &_,const del &__){
  15.         //return a.first>b.first || ( (a.first<b.first) && (a.second>b.second));
  16.         return (_.totalpath==__.totalpath)? ((_.position==__.position)? _.path.size()>=__.path.size():_.position>__.position):_.totalpath>__.totalpath;
  17.     }
  18. };
  19. void setgp(int t1,int t2,int t3,int t4){
  20.     if(t1>t2){
  21.         gp[t1][t2]=t4;
  22.         gp[t2][t1]=t3;
  23.     }else if(t1<t2){
  24.         gp[t1][t2]=t3;
  25.         gp[t2][t1]=t4;
  26.     }
  27. }
  28. int gpdistance(int x,int y){
  29.     return x<y? gp[x-1][y-1]:gp[y-1][x-1];
  30. }
  31. int gpspace(int x,int y){
  32.     return x>y? gp[x-1][y-1]:gp[y-1][x-1];
  33. }
  34. void gpspace(int x,int y,int change){
  35.     x>y? gp[x-1][y-1]+=change:gp[y-1][x-1]+=change;
  36. }
  37. int gpdeliever(int x){
  38.     return gp[x-1][x-1];
  39. }
  40. void gpdeliever(int x,int change){
  41.     gp[x-1][x-1]+=change;
  42. }
  43. void Order(){
  44.     int id,des,tspace,dis;
  45.     cin>>id>>des>>tspace;
  46.     traffic[id]=tspace;
  47.     if(gpdeliever(des)>0){
  48.         gpdeliever(des,-1);
  49.         del ans;
  50.         ans.id=id;
  51.         ans.position=des;
  52.         ans.path.emplace_back(des);
  53.         ans.totalpath=0;
  54.         mission.emplace(id,ans);
  55.         cout<<"Order "<<id<<" from: "<<des<<'\n';
  56.         return;
  57.     }
  58.     bool vis[101]={};
  59.     del temp,now,ans;
  60.     ans.totalpath=LLONG_MAX;
  61.     ans.position=-1;
  62.     temp.id=id;
  63.     temp.path.emplace_back(des);
  64.     temp.position=des;
  65.     temp.totalpath=0;
  66.     priority_queue<del,vector<del>,cmp> pq;
  67.     pq.emplace(temp);
  68.     while(!pq.empty()){
  69.         now=pq.top();
  70.         pq.pop();
  71.         if(vis[now.position]) continue;
  72.         vis[now.position]=true;
  73.         for(int i=1;i<=v;i++) if(!vis[i]&&gpspace(i,now.position)>=tspace){
  74.             dis=gpdistance(i,now.position);
  75.             if(dis>=0){
  76.                 temp.id=id;
  77.                 temp.path=now.path;
  78.                 temp.path.emplace_back(i);
  79.                 temp.totalpath=now.totalpath+dis;
  80.                 temp.position=i;
  81.                 pq.emplace(temp);
  82.                 if(ans.totalpath>temp.totalpath&&gpdeliever(temp.position)>0) ans=temp;
  83.             }
  84.         }
  85.     }
  86.     if(ans.position!=-1){
  87.         cout<<"Order "<<id<<" from: "<<ans.position<<'\n';
  88.         gpdeliever(ans.position,-1);
  89.         if(ans.path.size()>1){
  90.             int tt=ans.path[0];
  91.             for(int i=1;i<ans.path.size();i++){
  92.                 gpspace(tt,ans.path[i],(-tspace));
  93.                 tt=ans.path[i];
  94.             }
  95.         }
  96.         ans.position=des;
  97.         mission.emplace(id,ans);
  98.     }else{
  99.         cout<<"Just walk. T-T\n";
  100.     }
  101. }
  102. void Drop(){
  103.     int id,des,dis;
  104.     cin>>id>>des;
  105.     int tspace=traffic[id];
  106.     del now,temp=mission.find(id)->second,ans;
  107.     if(temp.path.size()>1){
  108.         int tt=temp.path[0];
  109.         for(int i=1;i<temp.path.size();i++){
  110.             gpspace(tt,temp.path[i],tspace);
  111.             tt=temp.path[i];
  112.         }
  113.     }
  114.     temp.path.clear();
  115.     if(temp.position==des){
  116.         cout<<"Order "<<temp.id<<" distance: "<<temp.totalpath<<'\n';
  117.         vector<int> path;
  118.         path.emplace_back(temp.position);
  119.         path2[id]=path;
  120.         return;
  121.     }
  122.     ans.totalpath=LLONG_MAX;
  123.     ans.position=-1;
  124.     bool vis[101]={};
  125.     priority_queue<del,vector<del>,cmp> pq;
  126.     temp.path.emplace_back(temp.position);
  127.     pq.emplace(temp);
  128.     while(!pq.empty()){
  129.         now=pq.top();
  130.         pq.pop();
  131.         if(vis[now.position]) continue;
  132.         vis[now.position]=true;
  133.         for(int i=1;i<=v;i++) if(!vis[i]&&gpspace(i,now.position)>=tspace){
  134.             dis=gpdistance(i,now.position);
  135.             if(dis>=0){
  136.                 temp.id=id;
  137.                 temp.path=now.path;
  138.                 temp.path.emplace_back(i);
  139.                 temp.totalpath=now.totalpath+dis;
  140.                 temp.position=i;
  141.                 pq.emplace(temp);
  142.                 if(ans.totalpath>temp.totalpath&&des==temp.position) ans=temp;
  143.             }
  144.         }
  145.     }
  146.     if(ans.position!=-1){
  147.         cout<<"Order "<<id<<" distance: "<<ans.totalpath<<'\n';
  148.         path2[id]=ans.path;
  149.         if(ans.path.size()>1){
  150.             int tt=ans.path[0];
  151.             for(int i=1;i<ans.path.size();i++){
  152.                 gpspace(ans.path[i],tt,(-tspace));
  153.                 tt=ans.path[i];
  154.             }
  155.         }
  156.     }else{
  157.         cout<<"No Way Home\n";
  158.         ans=mission.find(id)->second;
  159.         ans.path.clear();
  160.         ans.path.emplace_back(des);
  161.         drop.emplace(id,ans);
  162.     }
  163. }
  164. void Complete(){
  165.     int id;
  166.     cin>>id;
  167.     if(path2[id].size()){
  168.         if(path2[id].size()>1){
  169.             int tt=path2[id][0];
  170.             for(int i=1;i<path2[id].size();i++){
  171.                 gpspace(tt,path2[id][i],traffic[id]);
  172.                 tt=path2[id][i];
  173.             }
  174.         }
  175.         gpdeliever(path2[id].back(),1);
  176.         path2[id].clear();
  177.     }
  178.     vector<int> waitforerase;
  179.     for(auto i:drop){
  180.         id=i.first;
  181.         int tspace=traffic[id],des=i.second.path[0],dis;
  182.         bool vis[101]={};
  183.         del temp,now,ans;
  184.         ans.totalpath=LLONG_MAX;
  185.         ans.position=-1;
  186.         temp=i.second;
  187.         temp.path.clear();
  188.         temp.path.emplace_back(temp.position);
  189.         priority_queue<del,vector<del>,cmp> pq;
  190.         pq.emplace(temp);
  191.         while(!pq.empty()){
  192.             now=pq.top();
  193.             pq.pop();
  194.             if(vis[now.position]) continue;
  195.             vis[now.position]=true;
  196.             for(int i=1;i<=v;i++) if(!vis[i]&&gpspace(i,now.position)>=tspace){
  197.                 dis=gpdistance(i,now.position);
  198.                 if(dis>=0){
  199.                     temp.id=id;
  200.                     temp.path=now.path;
  201.                     temp.path.emplace_back(i);
  202.                     temp.totalpath=now.totalpath+dis;
  203.                     temp.position=i;
  204.                     pq.emplace(temp);
  205.                     if(ans.totalpath>temp.totalpath&&des==temp.position) ans=temp;
  206.                 }
  207.             }
  208.         }
  209.         if(ans.position!=-1){
  210.             cout<<"Order "<<id<<" distance: "<<ans.totalpath<<'\n';
  211.             if(ans.path.size()>1){
  212.                 int tt=ans.path[0];
  213.                 for(int i=1;i<ans.path.size();i++){
  214.                     gpspace(tt,ans.path[i],(-tspace));
  215.                     tt=ans.path[i];
  216.                 }
  217.             }
  218.             waitforerase.emplace_back(id);
  219.         }
  220.     }
  221.     for(int i:waitforerase){
  222.         drop.erase(i);
  223.     }
  224. }
  225. #undef int long long
  226. int main(){
  227.     long long t,t1,t2,t3,t4;
  228.     string s;
  229.     cin>>v>>e>>n;
  230.     for(int i=0;i<v;i++) for(int j=0;j<v;j++) gp[i][j]=-1;
  231.     for(int i=0;i<v;i++) gp[i][i]=0;
  232.     for(int i=0;i<n;i++){
  233.         cin>>s>>t1>>t2;
  234.         gp[t1-1][t1-1]=t2;
  235.     }
  236.     for(int i=0;i<e;i++){
  237.         cin>>s>>t1>>t2>>t3>>t4;
  238.         setgp(t1-1,t2-1,t3,t4);
  239.     }
  240.     cin>>t;
  241.     while(t--){
  242.         cin>>s;
  243.         if(s=="Order"){
  244.             Order();
  245.         }else if(s=="Drop"){
  246.             Drop();
  247.         }else if(s=="Complete"){
  248.             Complete();
  249.         }
  250.     }
  251.     return 0;
  252. }
  253. /*
  254. CS2351_DS_24Spring_HW4
  255. https://acm.cs.nthu.edu.tw/contest/3002/
  256. NTHU OJ 14311 - Spider-Man’s Delivery Service: Project Dijkstra
  257. AC code
  258. 2024 May 19
  259. 其實這個是透過測資缺陷通過的,用struct cmp的複雜規則剛好湊到答案的,但是我AC了所以沒人在乎
  260. */
Advertisement
Add Comment
Please, Sign In to add comment