Guest User

Problema 2 OJI 11-12 2017

a guest
Mar 11th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <fstream>
  2. #include <set>
  3. #include <algorithm>
  4. #include <map>
  5. #include <queue>
  6. #include <vector>
  7. #include <cstdlib>
  8. #include <string>
  9. #include <stack>
  10. #include <utility>
  11. #define NMAX 32000 // DE SCHIMBAT
  12. #define INF 0x3f3f3f3f
  13. #define HASHMOD 333011
  14. #define pb push_back
  15.  
  16. using namespace std;
  17.  
  18. ifstream fin("ninjago.in");
  19. ofstream fout("ninjago.out");
  20.  
  21. int p[NMAX],n,m,dist[NMAX],apm,coridoare,euri;
  22. vector<pair<int, pair<int, int> > > G[NMAX];
  23. pair<pair<int,int>, pair<int,int> > muchii[NMAX];
  24. char s[5];
  25. bool viz[NMAX];
  26.  
  27. int BFS() {
  28.     int i,x,nr=0,y;
  29.  
  30.     queue<int> Q;
  31.     viz[1]=1;
  32.     Q.push(1);
  33.     while(!Q.empty()) {
  34.         x=Q.front();
  35.         Q.pop();
  36.         ++nr;
  37.  
  38.         for(i=0;i<G[x].size();++i) {
  39.             y=G[x][i].first;
  40.             if(viz[y]==0 && G[x][i].second.second==0) {
  41.                 viz[y]=1;
  42.                 Q.push(y);
  43.             }
  44.         }
  45.     }
  46.  
  47.     return nr;
  48. }
  49.  
  50. int findSet(int x) {
  51.     if(p[x]==x) return x;
  52.  
  53.     return p[x]=findSet(p[x]);
  54. }
  55.  
  56. bool inSameSet(int x, int y) {
  57.     return findSet(x)==findSet(y);
  58. }
  59.  
  60. void unionSet(int x, int y) {
  61.     int i=findSet(x);
  62.     int j=findSet(y);
  63.  
  64.     p[i]=j;
  65. }
  66.  
  67. void kruskal() {
  68.     int i,x,y,cost,c,e;
  69.  
  70.     for(i=1;i<=n;++i) p[i]=i;
  71.     sort(muchii+1,muchii+m+1);
  72.  
  73.     for(i=1;i<=m;++i) {
  74.         c=muchii[i].first.first;
  75.         e=muchii[i].first.second;
  76.         x=muchii[i].second.first;
  77.         y=muchii[i].second.second;
  78.  
  79.         if(!inSameSet(x,y)) {
  80.             unionSet(x,y);
  81.             coridoare+=(c!=0);
  82.             euri+=c;
  83.             apm+=e;
  84.         }
  85.     }
  86. }
  87.  
  88. int main() { // NU LASA AFISARI PE ECRAN
  89.     int test,i,j,x,y,p5,cost,hasE;
  90.  
  91.     fin>>test;
  92.     fin>>n>>m;
  93.  
  94.     for(i=1;i<=m;++i) {
  95.         fin>>x>>y>>s;
  96.  
  97.         hasE=cost=0;
  98.         p5=1;
  99.         for(j=0;j<4;++j) {
  100.             if(s[j]=='E') ++hasE; //calculez numarul de E-uri pe fiecare muchie
  101.             else cost+=(s[j]-'A'+1)*p5; // puterea necesara pentru a distruge restul caracterelor
  102.             p5*=5;
  103.         }
  104.  
  105.         G[x].pb({y,{cost,hasE}});
  106.         G[y].pb({x,{cost,hasE}});
  107.         muchii[i]={{hasE,cost},{x,y}};
  108.     }
  109.  
  110.     if(test==1) fout<<BFS();
  111.     else {
  112.         kruskal();
  113.         if(test==2) fout<<coridoare<<'\n'<<euri;
  114.         else fout<<apm;
  115.     }
  116.  
  117.     fin.close();
  118.     fout.close();
  119.  
  120.     return 0;
  121. }
Add Comment
Please, Sign In to add comment