Advertisement
erfanul007

UVa 10496

Mar 10th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef unsigned long long int ull;
  5. typedef long long int ll;
  6. typedef vector<int> vi;
  7. typedef vector<ll> vll;
  8. typedef vector<vi> vvi;
  9. typedef pair<int,int > pii;
  10. typedef vector< pii > vpii;
  11. typedef set<int> sti;
  12.  
  13. #define sc scanf
  14. #define pf printf
  15. #define sci(n) scanf("%d",&n)
  16. #define scl(n) scanf("%lld",&n)
  17. #define scd(n) scanf("%lf",&n)
  18. #define scs(s) scanf("%s",s)
  19. #define pfi(n) printf("%d",n)
  20. #define pfl(n) printf("%lld",n)
  21. #define pff(n) cout<<n
  22. #define line printf("\n")
  23. #define spc printf(" ")
  24. #define loop(i,x,y) for(int i=int(x); i<=int(y); i++)
  25. #define rloop(i,y,x) for(int i=int(y); i>=int(x); i--)
  26. #define cspf(i) printf("Case %d: ", i)
  27. #define pb push_back
  28. #define mp make_pair
  29. #define ff first
  30. #define ss second
  31. #define all(v) v.begin(),v.end()
  32. #define rall(v) v.rbegin(),v.rend()
  33. #define read() freopen("input.txt", "r", stdin)
  34. #define write() freopen("output.txt", "w", stdout)
  35. /// Constants
  36. #define eps 1e-9
  37. #define PI acos(-1.0) // 3.1415926535897932
  38. #define MAX 1000009
  39.  
  40. bool chk[20];
  41. vpii vt;
  42. int mn=MAX;
  43.  
  44. void solve(int node,int sum,int n,int cnt)
  45. {
  46. if(cnt==n){
  47. sum+=(abs(vt[node].ff-vt[0].ff)+abs(vt[node].ss-vt[0].ss));
  48. mn=min(mn,sum);
  49. return;
  50. }
  51. loop(i,1,n){
  52. if(!chk[i]){
  53. chk[i]=1;
  54. int s=sum+(abs(vt[node].ff-vt[i].ff)+abs(vt[node].ss-vt[i].ss));
  55. solve(i,s,n,cnt+1);
  56. chk[i]=0;
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. read();
  64. write();
  65. int tc;
  66. cin>>tc;
  67. while(tc--){
  68. memset(chk,0,sizeof chk);
  69. vt.clear();
  70. mn=MAX;
  71. int row,col,a,b,n;
  72. cin>>row>>col;
  73. cin>>a>>b;
  74. chk[0]=1;
  75. vt.pb(mp(a,b));
  76. cin>>n;
  77. int x,y;
  78. loop(i,1,n){
  79. cin>>x>>y;
  80. vt.pb(mp(x,y));
  81. }
  82. solve(0,0,n,0);
  83. cout<<"The shortest path has length "<<mn<<endl;
  84. }
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement