Advertisement
Falak_Ahmed_Shakib

One Way Roads

Aug 18th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  6. typedef pair<ll,ll>pll;
  7. typedef pair<ll,pair<ll,ll>>plll;
  8. #define fi first
  9. #define se second
  10. #define pb push_back
  11. const int mod = 1e9+7;
  12. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  13. const int N=1e6;
  14. #define eb emplace_back
  15. //int X[] = {-1,0,1,1,1,0,-1,-1};
  16. //int Y[] = {-1,-1,-1,0,1,1,1,0};
  17. int X[] = {-1,1,0,0};
  18. int Y[] = {0,0,-1,1};
  19. bool iset(ll n, ll k)
  20. {
  21. if (n & (1ll << k))return true;
  22. else return false;
  23. }
  24.  
  25. vector<pll>adj[N+10];
  26. ll n;
  27. bool vis[N+10];
  28. ll cost[N+1];
  29. ll way=0;
  30. void dfs(ll s){
  31. vis[s]=true;
  32. for(auto x:adj[s]){
  33. ll node=x.fi;
  34. ll w=x.se;
  35. if(!vis[node]){
  36. way+=w;
  37. dfs(node);
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. fastread();
  44. ll t,cs=0;
  45. cin>>t;
  46. while(t--){
  47. cin>>n;
  48. map<pll,ll>mp;
  49. for(ll i=1;i<=n;i++){
  50. ll a,b,w;
  51. cin>>a>>b>>w;
  52. adj[a].pb({b,0});
  53. // mp[{a,b}]=0;
  54. adj[b].pb({a,w});
  55. if(a==1) mp[{b,a}]=w;
  56. }
  57.  
  58. vis[1]=true;
  59. ll way1=adj[1][0].se;
  60. way=0;
  61. dfs(adj[1][0].fi);
  62. // cout<<"way "<<way<<endl;
  63. way1+=way+mp[{adj[1][1].fi,1}];
  64. for(ll i=0;i<=n;i++){
  65. vis[i]=false;
  66. }
  67.  
  68. vis[1]=true;
  69. ll way2=adj[1][1].se;
  70. way=0;
  71. dfs(adj[1][1].fi);
  72. // cout<<"way "<<way<<endl;
  73. way2+=way+mp[{adj[1][0].fi,1}];
  74. // cout<<adj[1][0].fi<<" - "<<way1<<"\n "<<adj[1][1].fi<<" -> "<<way2<<endl;
  75. cout<<"Case "<<++cs<<": ";
  76. cout<<min(way1,way2)<<endl;
  77.  
  78. for(ll i=0;i<=n;i++){
  79. adj[i].clear();
  80. vis[i]=false;
  81. }
  82.  
  83. }
  84.  
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement