Advertisement
KuanTall

HEHEHE

Dec 27th, 2022
518
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CMake 6.88 KB | Jokes | 0 0
  1. Q1
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define INF 100000
  5.  
  6. struct Edge{
  7.     int a;
  8.     int b;
  9. };
  10.  
  11. int main(){
  12.     ios::sync_with_stdio(false);
  13.     cin.tie(0);
  14.     int e;
  15.     int all = 0;
  16.     int MAX = 1;
  17.     string u, v;
  18.     int a, b;
  19.  
  20.     while(cin>>e){
  21.         MAX = 1;
  22.         vector<string> name;
  23.         vector<vector<int>> cost;
  24.         vector<Edge> edges;
  25.  
  26.         for(int i = 0 ; i < e ; i++){
  27.             cin>>u>>v;
  28.             all = 0;
  29.             for(int j = 0 ; j < (int)name.size() ; j++){
  30.                 if(u == name[j]){
  31.                     all += 10;
  32.                     a = j;
  33.                 }
  34.                 else if(v == name[j]){
  35.                     all += 1;
  36.                     b = j;
  37.                 }
  38.                 if(all == 11)break;
  39.             }
  40.             if(u == v){
  41.                 b = a;
  42.                
  43.             }
  44.             if(all == 0){
  45.                 if(u == v){
  46.                     a = (int)name.size();
  47.                     name.push_back(u);
  48.                     b = a;
  49.                 }
  50.                 else{
  51.                     a = (int)name.size();
  52.                     name.push_back(u);
  53.                     b = (int)name.size();
  54.                     name.push_back(v);
  55.                 }
  56.             }
  57.             else if(all == 1){
  58.                 a = (int)name.size();
  59.                 name.push_back(u);
  60.             }
  61.             else if(all == 10){
  62.                 if(u != v){
  63.                 b = (int)name.size();
  64.                 name.push_back(v);
  65.                 }
  66.             }
  67.             if(a != b)
  68.             edges.push_back({a, b});
  69.         }
  70.  
  71.         cost.resize((int)name.size());
  72.         for(int i = 0 ; i < (int)name.size() ; i++){
  73.             cost[i].resize((int)name.size(), INF);
  74.             cost[i][i] = 0;
  75.         }
  76.  
  77.         for(int i = 0 ; i < (int)edges.size() ; i++){
  78.             cost[edges[i].a][edges[i].b] = 1;
  79.             cost[edges[i].b][edges[i].a] = 1;
  80.         }
  81.  
  82.         for(int i = 0 ; i < (int)cost.size() ; i++){
  83.             for(int j = 0 ; j < (int)cost.size() ; j++){
  84.                 for(int k = 0 ; k < (int)cost.size() ; k++){
  85.                     if(cost[j][k] > (cost[j][i] + cost[i][k])){
  86.                         cost[j][k] = cost[j][i] + cost[i][k];
  87.                         //MAX = max(MAX, cost[j][k]);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.  
  93.         /*for(int i = 0 ; i < cost.size() ; i++){
  94.             for(int j = 0 ; j < cost.size() ; j++){
  95.                 cout<<cost[i][j]<<" ";
  96.             }
  97.             cout<<endl;
  98.         }*/
  99.         bool TF = true;
  100.         for(int i = 0 ; i < (int)cost.size() ; i++){
  101.             for(int j = 0 ; j < (int)cost.size() ; j++){
  102.                 if(cost[i][j] == INF){
  103.                     MAX = -1;
  104.                     TF = false;
  105.                     break;
  106.                 }
  107.                 MAX = max(MAX, cost[i][j]);
  108.             }
  109.             if(TF == false)break;
  110.         }
  111.         cout<<MAX<<"\n";
  112.     }
  113.  
  114.     return 0;
  115. }
  116. Q2
  117. #include <bits/stdc++.h>
  118. using namespace std;
  119.  
  120. int main(){
  121.     int l, r, s;
  122.     while(cin>>l>>r>>s){
  123.         vector<long> locrad;
  124.         vector<pair<int, long>> adj[l];
  125.         vector<int> shelter;
  126.         int start;
  127.         long rad;
  128.         int a, b, c;
  129.  
  130.         for(int i = 0 ; i < l ; i++){
  131.             cin>>rad;
  132.             locrad.push_back(rad);
  133.         }
  134.         for(int i = 0 ; i < r ; i++){
  135.             cin>>a>>b>>c;
  136.             adj[a].push_back({b, c});
  137.             adj[b].push_back({a, c});
  138.         }
  139.         for(int i = 0 ; i < s ; i++){
  140.             cin>>a;
  141.             shelter.push_back(a);
  142.         }
  143.         cin>>start;
  144.  
  145.         vector<long> cumrad(l, INT_MAX);
  146.         cumrad[start] = locrad[start];
  147.  
  148.         priority_queue<pair<int, long>, vector<pair<int, long>>, greater<pair<int, int>>> pq;
  149.         pq.push({locrad[start], start});
  150.         while(!pq.empty()){
  151.             int a = pq.top().second;
  152.             pq.pop();
  153.             for(auto u : adj[a]){
  154.                 int b = u.first, w = u.second;
  155.                 if(cumrad[a] + w + locrad[b] < cumrad[b]){
  156.                     cumrad[b] = cumrad[a] + w + locrad[b];
  157.                     pq.push({cumrad[b], b});
  158.                 }
  159.             }
  160.         }
  161.         int minrad = INT_MAX;
  162.  
  163.         for(int i = 0 ; i < s ; i++){
  164.             minrad = min((long)minrad, cumrad[shelter[i]]);
  165.         }
  166.         if(minrad >= INT_MAX)minrad = -1;
  167.  
  168.         cout<<minrad<<"\n";
  169.     }
  170.  
  171.     return 0;
  172. }
  173. Q3
  174. #include <bits/stdc++.h>
  175. using namespace std;
  176. int n, m, c, r;
  177.  
  178. bool BFS(vector<vector<int>> &rGraph, int s, int t, vector<int> &parent){
  179.     vector<bool> visited(rGraph.size(), false);
  180.  
  181.     queue<int> q;
  182.     q.push(s);
  183.     visited[s] = true;
  184.     parent[s] = -1;
  185.  
  186.     int u;
  187.     while(!q.empty()){
  188.         u = q.front();
  189.         q.pop();
  190.         for(int v = 0 ; v < (int)rGraph.size() ; ++v){
  191.             if(!visited[v] && rGraph[u][v] > 0){
  192.                 q.push(v);
  193.                 parent[v] = u;
  194.                 visited[v] = true;
  195.             }
  196.         }
  197.     }
  198.  
  199.     return visited[t] == true;
  200. }
  201.  
  202. int FF(vector<vector<int>> &graph, int s, int t){
  203.     int u, v;
  204.     vector<vector<int>> rGraph(n+m+2);
  205.     for(int i = 0 ; i < (int)rGraph.size() ; i++){
  206.         rGraph[i].resize(n+m+2);
  207.     }
  208.  
  209.     for(u = 0 ; u < (int)rGraph.size() ; u++){
  210.         for(v = 0 ; v < (int)rGraph.size() ; v++){
  211.             rGraph[u][v] = graph[u][v];
  212.         }
  213.     }
  214.  
  215.     vector<int> parent(rGraph.size());
  216.     int max_flow = 0;
  217.  
  218.     while(BFS(rGraph, s, t, parent)){
  219.         int path_flow = INT_MAX;
  220.  
  221.         for(v = t ; v != s ; v = parent[v]){
  222.             u = parent[v];
  223.             path_flow = min(path_flow, rGraph[u][v]);
  224.         }
  225.  
  226.         for (v = t; v != s; v = parent[v])
  227.         {
  228.             u = parent[v];
  229.             rGraph[u][v] -= path_flow;
  230.             rGraph[v][u] += path_flow;
  231.         }
  232.  
  233.         max_flow += path_flow;
  234.     }
  235.  
  236.     return max_flow;
  237. }
  238.  
  239. int main(){
  240.     ios::sync_with_stdio(false);
  241.     cin.tie(0);
  242.     while(cin>>n>>m>>c>>r){
  243.         vector<vector<int>> graph(n+m+2);
  244.         for(int i = 0 ; i < (int)graph.size() ; i++){
  245.             graph[i].resize(graph.size(), 0);
  246.         }
  247.         int student, num, time;
  248.         for(int i = 1 ; i <= m ; i++){
  249.             graph[0][i] = 1;
  250.             cin>>student>>num;
  251.             for(int j = 0 ; j < num ; j++){
  252.                 cin>>time;
  253.                 graph[i][m+1+time] = 1;
  254.             }
  255.         }
  256.  
  257.         int ts;
  258.         for(int i = 0 ; i < r ; i++){
  259.             for(int j = 0 ; j < n ; j++){
  260.                 graph[m+1+j][m+n+1] = 0;
  261.             }
  262.             cin>>ts;
  263.             for(int j = 0 ; j < ts ; j++){
  264.                 cin>>time;
  265.                 graph[m+1+time][m+n+1] = c;
  266.             }
  267.             cout<<m-FF(graph, 0, m+n+1)<<"\n";
  268.         }
  269.     }
  270.  
  271.     return 0;
  272. }
  273.  
Advertisement
Comments
  • BK5008
    1 year
    # C++ 2.57 KB | 0 0
    1. #include <bits/stdc++.h>
    2. using namespace std;
    3.  
    4. struct edge
    5. {
    6.     int a;
    7.     int b;
    8. };
    9.  
    10. void sol(vector<string> id, vector<edge> edges)
    11. {
    12.     int n = id.size(), m = edges.size(), MAX = 1;
    13.     bool flag = true;
    14.     vector<vector<int>> cost;
    15.     cost.resize(n);
    16.     for(int i=0;i<n;i++){
    17.         cost[i].resize(n, INT_MAX);
    18.         cost[i][i] = 0;
    19.     }
    20.     for(int i=0;i<m;i++){
    21.         cost[edges[i].a][edges[i].b] = 1;
    22.         cost[edges[i].b][edges[i].a] = 1;
    23.     }
    24.     for(int i=0;i<(int)cost.size();i++){
    25.         for(int j=0;j<(int)cost.size();j++){
    26.             for(int k=0;k<(int)cost.size();k++){
    27.                 if((cost[j][i]+cost[i][k])<cost[j][k]){
    28.                     cost[j][k] = cost[j][i] + cost[i][k];
    29.                 }
    30.             }
    31.         }
    32.     }
    33.     for(int i=0;i<(int)cost.size();i++){
    34.         for(int j=0;j<(int)cost.size();j++){
    35.             cout << cost[i][j] << " ";
    36.         }
    37.     }
    38.     for(int i=0;i<(int)cost.size();i++){
    39.         for(int j=0;j<(int)cost.size();j++){
    40.             if(cost[i][j] == INT_MAX){
    41.                 flag = false;
    42.                 MAX = -1;
    43.                 break;
    44.             }
    45.             MAX = max(MAX, cost[i][j]);
    46.         }
    47.         if(!flag){
    48.             break;
    49.         }
    50.     }
    51.     cout << MAX << "\n";
    52.     return;
    53. }
    54.  
    55. int main()
    56. {
    57.     int n, a, b, idx=0;
    58.     while(cin >> n){
    59.         vector<string> id;
    60.         vector<edge> edges;
    61.         string s1, s2;
    62.         for(int i=0;i<n;i++){
    63.             idx = 0;
    64.             cin >> s1 >> s2;
    65.             for(int j=0;j<(int)id.size();j++){
    66.                 if(s1==id[j]){
    67.                     idx += 10;
    68.                     a = j;
    69.                 }
    70.                 if(s2==id[j]){
    71.                     idx += 1;
    72.                     b = j;
    73.                 }
    74.             }
    75.             if(s1==s2 && idx==0){
    76.                 a = (int)id.size();
    77.                 id.push_back(s1);
    78.                 b = a;
    79.             }
    80.             else{
    81.                 if(idx==0){
    82.                     a = (int)id.size();
    83.                     id.push_back(s1);
    84.                     b = (int)id.size();
    85.                     id.push_back(s2);
    86.                 }
    87.                 else if(idx==1){
    88.                     a = (int)id.size();
    89.                     id.push_back(s1);
    90.                 }
    91.                 else if(idx>1){
    92.                     b = (int)id.size();
    93.                     id.push_back(s2);
    94.                 }
    95.                 if(a!=b){
    96.                     edges.push_back({a, b});
    97.                 }
    98.             }
    99.         }
    100.         sol(id, edges);
    101.     }
    102.     return 0;
    103. }
Add Comment
Please, Sign In to add comment
Advertisement