Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q1
- #include <bits/stdc++.h>
- using namespace std;
- #define INF 100000
- struct Edge{
- int a;
- int b;
- };
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- int e;
- int all = 0;
- int MAX = 1;
- string u, v;
- int a, b;
- while(cin>>e){
- MAX = 1;
- vector<string> name;
- vector<vector<int>> cost;
- vector<Edge> edges;
- for(int i = 0 ; i < e ; i++){
- cin>>u>>v;
- all = 0;
- for(int j = 0 ; j < (int)name.size() ; j++){
- if(u == name[j]){
- all += 10;
- a = j;
- }
- else if(v == name[j]){
- all += 1;
- b = j;
- }
- if(all == 11)break;
- }
- if(u == v){
- b = a;
- }
- if(all == 0){
- if(u == v){
- a = (int)name.size();
- name.push_back(u);
- b = a;
- }
- else{
- a = (int)name.size();
- name.push_back(u);
- b = (int)name.size();
- name.push_back(v);
- }
- }
- else if(all == 1){
- a = (int)name.size();
- name.push_back(u);
- }
- else if(all == 10){
- if(u != v){
- b = (int)name.size();
- name.push_back(v);
- }
- }
- if(a != b)
- edges.push_back({a, b});
- }
- cost.resize((int)name.size());
- for(int i = 0 ; i < (int)name.size() ; i++){
- cost[i].resize((int)name.size(), INF);
- cost[i][i] = 0;
- }
- for(int i = 0 ; i < (int)edges.size() ; i++){
- cost[edges[i].a][edges[i].b] = 1;
- cost[edges[i].b][edges[i].a] = 1;
- }
- for(int i = 0 ; i < (int)cost.size() ; i++){
- for(int j = 0 ; j < (int)cost.size() ; j++){
- for(int k = 0 ; k < (int)cost.size() ; k++){
- if(cost[j][k] > (cost[j][i] + cost[i][k])){
- cost[j][k] = cost[j][i] + cost[i][k];
- //MAX = max(MAX, cost[j][k]);
- }
- }
- }
- }
- /*for(int i = 0 ; i < cost.size() ; i++){
- for(int j = 0 ; j < cost.size() ; j++){
- cout<<cost[i][j]<<" ";
- }
- cout<<endl;
- }*/
- bool TF = true;
- for(int i = 0 ; i < (int)cost.size() ; i++){
- for(int j = 0 ; j < (int)cost.size() ; j++){
- if(cost[i][j] == INF){
- MAX = -1;
- TF = false;
- break;
- }
- MAX = max(MAX, cost[i][j]);
- }
- if(TF == false)break;
- }
- cout<<MAX<<"\n";
- }
- return 0;
- }
- Q2
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int l, r, s;
- while(cin>>l>>r>>s){
- vector<long> locrad;
- vector<pair<int, long>> adj[l];
- vector<int> shelter;
- int start;
- long rad;
- int a, b, c;
- for(int i = 0 ; i < l ; i++){
- cin>>rad;
- locrad.push_back(rad);
- }
- for(int i = 0 ; i < r ; i++){
- cin>>a>>b>>c;
- adj[a].push_back({b, c});
- adj[b].push_back({a, c});
- }
- for(int i = 0 ; i < s ; i++){
- cin>>a;
- shelter.push_back(a);
- }
- cin>>start;
- vector<long> cumrad(l, INT_MAX);
- cumrad[start] = locrad[start];
- priority_queue<pair<int, long>, vector<pair<int, long>>, greater<pair<int, int>>> pq;
- pq.push({locrad[start], start});
- while(!pq.empty()){
- int a = pq.top().second;
- pq.pop();
- for(auto u : adj[a]){
- int b = u.first, w = u.second;
- if(cumrad[a] + w + locrad[b] < cumrad[b]){
- cumrad[b] = cumrad[a] + w + locrad[b];
- pq.push({cumrad[b], b});
- }
- }
- }
- int minrad = INT_MAX;
- for(int i = 0 ; i < s ; i++){
- minrad = min((long)minrad, cumrad[shelter[i]]);
- }
- if(minrad >= INT_MAX)minrad = -1;
- cout<<minrad<<"\n";
- }
- return 0;
- }
- Q3
- #include <bits/stdc++.h>
- using namespace std;
- int n, m, c, r;
- bool BFS(vector<vector<int>> &rGraph, int s, int t, vector<int> &parent){
- vector<bool> visited(rGraph.size(), false);
- queue<int> q;
- q.push(s);
- visited[s] = true;
- parent[s] = -1;
- int u;
- while(!q.empty()){
- u = q.front();
- q.pop();
- for(int v = 0 ; v < (int)rGraph.size() ; ++v){
- if(!visited[v] && rGraph[u][v] > 0){
- q.push(v);
- parent[v] = u;
- visited[v] = true;
- }
- }
- }
- return visited[t] == true;
- }
- int FF(vector<vector<int>> &graph, int s, int t){
- int u, v;
- vector<vector<int>> rGraph(n+m+2);
- for(int i = 0 ; i < (int)rGraph.size() ; i++){
- rGraph[i].resize(n+m+2);
- }
- for(u = 0 ; u < (int)rGraph.size() ; u++){
- for(v = 0 ; v < (int)rGraph.size() ; v++){
- rGraph[u][v] = graph[u][v];
- }
- }
- vector<int> parent(rGraph.size());
- int max_flow = 0;
- while(BFS(rGraph, s, t, parent)){
- int path_flow = INT_MAX;
- for(v = t ; v != s ; v = parent[v]){
- u = parent[v];
- path_flow = min(path_flow, rGraph[u][v]);
- }
- for (v = t; v != s; v = parent[v])
- {
- u = parent[v];
- rGraph[u][v] -= path_flow;
- rGraph[v][u] += path_flow;
- }
- max_flow += path_flow;
- }
- return max_flow;
- }
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- while(cin>>n>>m>>c>>r){
- vector<vector<int>> graph(n+m+2);
- for(int i = 0 ; i < (int)graph.size() ; i++){
- graph[i].resize(graph.size(), 0);
- }
- int student, num, time;
- for(int i = 1 ; i <= m ; i++){
- graph[0][i] = 1;
- cin>>student>>num;
- for(int j = 0 ; j < num ; j++){
- cin>>time;
- graph[i][m+1+time] = 1;
- }
- }
- int ts;
- for(int i = 0 ; i < r ; i++){
- for(int j = 0 ; j < n ; j++){
- graph[m+1+j][m+n+1] = 0;
- }
- cin>>ts;
- for(int j = 0 ; j < ts ; j++){
- cin>>time;
- graph[m+1+time][m+n+1] = c;
- }
- cout<<m-FF(graph, 0, m+n+1)<<"\n";
- }
- }
- return 0;
- }
Advertisement
Comments
-
- #include <bits/stdc++.h>
- using namespace std;
- struct edge
- {
- int a;
- int b;
- };
- void sol(vector<string> id, vector<edge> edges)
- {
- int n = id.size(), m = edges.size(), MAX = 1;
- bool flag = true;
- vector<vector<int>> cost;
- cost.resize(n);
- for(int i=0;i<n;i++){
- cost[i].resize(n, INT_MAX);
- cost[i][i] = 0;
- }
- for(int i=0;i<m;i++){
- cost[edges[i].a][edges[i].b] = 1;
- cost[edges[i].b][edges[i].a] = 1;
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- for(int k=0;k<(int)cost.size();k++){
- if((cost[j][i]+cost[i][k])<cost[j][k]){
- cost[j][k] = cost[j][i] + cost[i][k];
- }
- }
- }
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- cout << cost[i][j] << " ";
- }
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- if(cost[i][j] == INT_MAX){
- flag = false;
- MAX = -1;
- break;
- }
- MAX = max(MAX, cost[i][j]);
- }
- if(!flag){
- break;
- }
- }
- cout << MAX << "\n";
- return;
- }
- int main()
- {
- int n, a, b, idx=0;
- while(cin >> n){
- vector<string> id;
- vector<edge> edges;
- string s1, s2;
- for(int i=0;i<n;i++){
- idx = 0;
- cin >> s1 >> s2;
- for(int j=0;j<(int)id.size();j++){
- if(s1==id[j]){
- idx += 10;
- a = j;
- }
- if(s2==id[j]){
- idx += 1;
- b = j;
- }
- }
- if(s1==s2 && idx==0){
- a = (int)id.size();
- id.push_back(s1);
- b = a;
- }
- else{
- if(idx==0){
- a = (int)id.size();
- id.push_back(s1);
- b = (int)id.size();
- id.push_back(s2);
- }
- else if(idx==1){
- a = (int)id.size();
- id.push_back(s1);
- }
- else if(idx>1){
- b = (int)id.size();
- id.push_back(s2);
- }
- if(a!=b){
- edges.push_back({a, b});
- }
- }
- }
- sol(id, edges);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment
Advertisement