Advertisement
Guest User

CF

a guest
Oct 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<cstdlib>
  4.  
  5. #define pf printf
  6. #define sf scanf
  7. #define pi 3.141592653589793116
  8. #define INF 99999
  9. #define maxInt 99999999
  10. #define maxNEG -99999999.0
  11. #define debug cout<<"Hello here!"<<endl;
  12. #define readFile freopen("in.txt","r",stdin);
  13.  
  14. #define NULL_VALUE -999999
  15. #define INFINITY 999999
  16. #define MAX_HEAP_SIZE 100000
  17. #define MAXREAL 999999.0
  18.  
  19.  
  20. using namespace std;
  21.  
  22. /**
  23.     Keeping track of edge and weight
  24. */
  25. class edge{
  26. public:
  27.     int dest;
  28.     float weight;
  29.  
  30.     edge(){
  31.         dest=0;
  32.         weight=0;
  33.     }
  34.  
  35.     edge(int v,float w){
  36.         dest=v;
  37.         weight=w;
  38.     }
  39. };
  40.  
  41.  
  42. //****************Dynamic ArrayList class based************************
  43. class ArrayList
  44. {
  45.     edge * list;
  46.     int length ;
  47.     int listMaxSize ;
  48.     int listInitSize ;
  49. public:
  50.     ArrayList() ;
  51.     ~ArrayList() ;
  52.     int searchItem(edge item) ;
  53.     void insertItem(edge item) ;
  54.     void removeItem(edge item) ;
  55.     void removeItemAt(int item);
  56.     edge getItem(int position) ;
  57.     int getLength();
  58.     void reset(){
  59.         free(list);
  60.         listInitSize=512;
  61.         listMaxSize=listInitSize;
  62.         length = 0 ;
  63.     }
  64.  
  65.     bool empty();
  66.     void printList();
  67.  
  68.     void reallocList(){
  69.         listInitSize = 4 ;
  70.         listMaxSize = listInitSize ;
  71.         list = new edge[listMaxSize] ;
  72.     }
  73. } ;
  74.  
  75. //Initializing list
  76. ArrayList::ArrayList()
  77. {
  78.     listInitSize = 512 ;
  79.     listMaxSize = listInitSize ;
  80.     list = new edge[listMaxSize] ;
  81.     length = 0 ;
  82. }
  83.  
  84. void ArrayList::insertItem(edge newEdge)
  85. {
  86.     edge * tempList ;
  87.     if (length == listMaxSize)
  88.     {
  89.         //allocate new memory space for tempList
  90.         listMaxSize = 2 * listMaxSize ;
  91.         tempList = new edge[listMaxSize] ;
  92.         int i;
  93.         for( i = 0; i < length ; i++ )
  94.         {
  95.             tempList[i] = list[i] ; //copy all items from list to tempList
  96.         }
  97.         delete[] list ; //free the memory allocated before
  98.         list = tempList ; //make list to point to new memory
  99.     };
  100.  
  101.     list[length] = newEdge ; //store new item
  102.     length++ ;
  103. }
  104.  
  105. int ArrayList::searchItem(edge item)
  106. {
  107.     if(length==0) return NULL_VALUE;
  108.     int i = 0;
  109.     for (i = 0; i < length; i++)
  110.     {
  111.         if( list[i].dest == item.dest && list[i].weight == item.weight ) {
  112.             return i;
  113.         }
  114.     }
  115.     return NULL_VALUE;
  116. }
  117.  
  118. void ArrayList::removeItemAt(int position) //do not preserve order of items
  119. {
  120.     if ( position < 0 || position >= length ) return ; //nothing to remove
  121.     list[position] = list[length-1] ;
  122.     length-- ;
  123. }
  124.  
  125.  
  126. void ArrayList::removeItem(edge item)
  127. {
  128.     int position;
  129.     position = searchItem(item) ;
  130.     if ( position == NULL_VALUE ) return ; //nothing to remove
  131.     removeItemAt(position) ;
  132. }
  133.  
  134.  
  135. edge ArrayList::getItem(int position)
  136. {
  137.     if(position < 0 || position >= length) {
  138.         //cout<< "No such edge found....\n";
  139.         edge temp(0,0);
  140.         return temp;
  141.     }
  142.     return list[position] ;
  143. }
  144.  
  145. int ArrayList::getLength()
  146. {
  147.     return length ;
  148. }
  149.  
  150. bool ArrayList::empty()
  151. {
  152.     if(length==0)return true;
  153.     else return false;
  154. }
  155.  
  156. void ArrayList::printList()
  157. {
  158.     int i;
  159.     for(i=0;i<length;i++)
  160.         printf("Vertex: %d, Edge: %d", list[i].dest,list[i].weight);
  161.     printf("Current size: %d, current length: %d\n", listMaxSize, length);
  162. }
  163.  
  164. ArrayList::~ArrayList()
  165. {
  166.     if(list) delete [] list;
  167.     list = 0 ;
  168. }
  169.  
  170. //******************ArrayList class ends here*************************
  171.  
  172. //******************Graph class starts here**************************
  173. class Graph
  174. {
  175.  
  176.     int nVertices, nEdges ;
  177.     bool directed ;
  178.     ArrayList  * adjList ;
  179.     int **cost;
  180.     int **w;
  181.     //define other variables required for bfs such as color, parent, and dist
  182.     //you must use pointers and dynamic allocation
  183.  
  184. public:
  185.     int vtrack;
  186.     Graph(bool dir=false);
  187.     ~Graph();
  188.     void setnVertices(int n);
  189.     void addEdge(int u, int v,int w);
  190.     void removeEdge(int u, int v,int w);
  191.     void printGraph();
  192.  
  193.     void removeIT(int n){
  194.         if(vtrack==1) return;
  195.         for(int i=1;i<=nVertices;i++)
  196.             adjList[i].removeItem(edge(n,cost[i][n]));
  197.         adjList[n].reset();
  198.         vtrack--;
  199.     }
  200.  
  201.     void setnEdges(int n){
  202.         //eLength=0;
  203.         //nEdges=n;
  204.  
  205.         //e=new edgeExtra[n];
  206.         cost=new int*[n + 1];
  207.         for(int i=0;i<=n;i++) cost[i]=new int[n +1];
  208.         for(int i=0;i<=n;i++)
  209.             for(int j=0;j<=n;j++)
  210.             cost[i][j]=INFINITY;
  211.     }
  212.  
  213.  
  214.     void Floyad_warshall(){
  215.         //Initializing
  216.         long long int ans=0;
  217.  
  218.         for(int i=0;i<=nVertices;i++)
  219.             for(int j=0;j<=nVertices;j++)
  220.             {
  221.                 if(i==j) w[i][j]=0;
  222.                 else
  223.                     w[i][j]=INF;
  224.             }
  225.  
  226.  
  227.         for(int i=1;i<=nVertices;i++){
  228.             for(int j=0;j<adjList[i].getLength();j++){
  229.                 int des,wei;
  230.                 des=adjList[i].getItem(j).dest;
  231.                 wei=adjList[i].getItem(j).weight;
  232.                 if(des >nVertices or des<1) continue;
  233.                 w[i][des]=wei;
  234.             }
  235.         }
  236.  
  237.         for(int k=1;k<=nVertices;k++)
  238.         {
  239.             for(int i=1;i<=nVertices;i++)
  240.             {
  241.                 for(int j=1;j<=nVertices;j++)
  242.                 {
  243.                     if(w[i][k]==INF or w[k][j]==INF) continue;
  244.                     if(w[i][j] > w[i][k] + w[k][j] )
  245.                     {
  246.                         w[i][j]=w[i][k] +w[k][j];
  247.  
  248.                     }
  249.                 }
  250.             }
  251.         }
  252.  
  253.         for(int i=1;i<=nVertices;i++){
  254.             for(int j=1;j<=nVertices;j++){
  255.                 if(w[i][j]==INF) continue;
  256.                 else ans+=w[i][j];
  257.             }
  258.  
  259.         }
  260.  
  261.         cout<<ans<<endl;
  262.  
  263.     }
  264.  
  265. };
  266.  
  267.  
  268. Graph::Graph(bool dir)
  269. {
  270.     //Initializing the graph
  271.     w=0;
  272.     cost=0;
  273.     nVertices = 0 ;
  274.     nEdges = 0 ;
  275.     adjList = 0 ;
  276.     directed =dir;
  277.     vtrack=0;
  278.  
  279. }
  280.  
  281. void Graph::setnVertices(int n)
  282. {
  283.     this->nVertices = n ;
  284.     vtrack=n;
  285.  
  286.     if(adjList!=0) delete[] adjList ; //delete previous list
  287.     //delete previous record
  288.  
  289.     adjList = new ArrayList[nVertices+1] ;
  290.     w=new int*[nVertices +1];
  291.     for(int i=0;i<=nVertices;i++) {
  292.         w[i]=new int[nVertices + 1];
  293.     }
  294.  
  295.     //cout<< "Initializing vertices at 1\n" ;
  296. }
  297.  
  298. void Graph::addEdge(int u, int v,int w)
  299. {
  300.     if(u<=0 || v<=0 || u>nVertices || v>nVertices) {
  301.         //cout<< "Vertices out of range\n";
  302.         return;
  303.     } //vertex out of range
  304.     //this->nEdges++ ;
  305.     adjList[u].insertItem(edge(v,w)) ;
  306.     cost[u][v]=w;
  307.  
  308.     //if(!directed) adjList[v].insertItem(edge(u,w)) ;
  309. }
  310.  
  311. void Graph::removeEdge(int u, int v,int w)
  312. {
  313.     if(u<1 || u>nVertices || v<1 || v>nVertices){
  314.         //cout<< "Out of bound...\n";
  315.         return;
  316.     }
  317.     //write this function
  318.     adjList->removeItem(edge(u,w));
  319.     adjList->removeItem(edge(v,w));
  320. }
  321.  
  322.  
  323. void Graph::printGraph()
  324. {
  325.     printf("\nNumber of vertices: %d, Number of edges: %d\n", nVertices, nEdges);
  326.     for(int i=0;i<nVertices;i++)
  327.     {
  328.         printf("%d:", i);
  329.         for(int j=0; j<adjList[i].getLength();j++)
  330.         {
  331.             printf(" Vertex: %d, Edge: %d", adjList[i].getItem(j).dest,adjList[i].getItem(j).weight);
  332.         }
  333.         printf("\n");
  334.     }
  335. }
  336.  
  337.  
  338. Graph::~Graph()
  339. {
  340.     if(adjList!=0) delete[] adjList ; //delete previous list
  341. }
  342.  
  343. int edgeCount(int n){
  344.     //nCr
  345.     return (n*(n-1));
  346. }
  347.  
  348. int main(){
  349.  
  350.     //freopen("test.txt", "r",stdin);
  351.  
  352.     int n,m,x;
  353.     int u,v,w;
  354.  
  355.     Graph g;
  356.  
  357.     scanf("%d",&n);
  358.     if(n==1) m=1;
  359.     else m=edgeCount(n);
  360.     //cout<< "m: "<<m<<endl;
  361.  
  362.     g.setnVertices(n);
  363.     g.setnEdges(m);
  364.  
  365.     for(int i=1;i<=n;i++){
  366.         for(int j=1;j<=n;j++){
  367.             sf("%d",&x);
  368.             if(i==j) continue;
  369.             else{
  370.                 g.addEdge(i,j,x);
  371.             }
  372.         }
  373.     }
  374.  
  375.     for(int i=0;i<n;i++)
  376.     {
  377.         g.Floyad_warshall();
  378.         sf("%d",&x);
  379.         if(g.vtrack==1) break;
  380.         cout<< "Vtrack: "<<g.vtrack<<endl;
  381.         g.removeIT(x);
  382.     }
  383.  
  384.     return 0;
  385.  
  386. }
  387.  
  388. /**
  389. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement