Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <unistd.h>
  4.  
  5. #include <stdlib.h>
  6.  
  7.  
  8.  
  9. typedef struct vertex{
  10.  
  11. int id;
  12.  
  13. struct arch* arches;
  14.  
  15. struct arch* last;
  16.  
  17. struct vertex* last_pop;
  18.  
  19. }vertexT;
  20.  
  21.  
  22.  
  23. typedef struct arch{
  24.  
  25. struct vertex* connects;
  26.  
  27. struct arch* next;
  28.  
  29. }archT;
  30.  
  31.  
  32.  
  33. typedef struct graph{
  34.  
  35. struct vertex* vertices;
  36.  
  37. }graphT;
  38.  
  39.  
  40.  
  41. typedef struct stackElement{
  42.  
  43. struct vertex* vertex;
  44.  
  45. }stackE;
  46.  
  47.  
  48.  
  49. typedef struct stackPile{
  50.  
  51. int top;
  52.  
  53. struct stackElement* contents;
  54.  
  55. }stackP;
  56.  
  57. typedef struct fluxTable{
  58. int** flux;
  59. }fluxT;
  60.  
  61.  
  62.  
  63.  
  64.  
  65. void stack_init(stackP* stack, int size){
  66.  
  67. stackE* contents = (stackE*)malloc(sizeof(stackE) * size);
  68.  
  69. stack->contents=contents;
  70.  
  71. stack->top=-1;
  72.  
  73. }
  74.  
  75.  
  76.  
  77. void push(stackP* stack, vertexT* v){
  78.  
  79. stack->contents[++stack->top].vertex=v;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. vertexT* pop(stackP* stack){
  86.  
  87. return stack->contents[stack->top--].vertex;
  88.  
  89. }
  90.  
  91. void stack_clean(stackP* stack){
  92. stack->top=-1;
  93. }
  94.  
  95. int is_empty(stackP* stack){
  96. if(stack->top==-1)
  97. return 1;
  98. return 0;
  99. }
  100.  
  101.  
  102.  
  103. void graph_init(graphT* graph, int n){
  104.  
  105. vertexT* vertices=(vertexT*)malloc(sizeof(vertexT) * n);
  106.  
  107. graph->vertices=vertices;
  108.  
  109. }
  110.  
  111. void flux_init(int** flux, int n){
  112. flux=(int**)malloc(sizeof(int*) * n);
  113. int i, j;
  114. for(i=0; i<n; i++){
  115. flux[i]=(int*)malloc(sizeof(int) * n);
  116. for(j=0; j<n; j++)
  117. flux[i][j]=0;
  118. }
  119. }
  120.  
  121.  
  122.  
  123. void add_adjacency(graphT* graph, int origin, int destination){
  124.  
  125. archT* arch=(archT*)malloc(sizeof(archT));
  126.  
  127. arch->next=NULL;
  128.  
  129. arch->connects=&graph->vertices[destination];
  130.  
  131.  
  132.  
  133. vertexT* v=&graph->vertices[origin];
  134.  
  135. if(v->arches!=NULL){
  136.  
  137. v->last->next=arch;
  138.  
  139. v->last=arch;
  140.  
  141. }
  142.  
  143. else{
  144.  
  145. v->arches=arch;
  146.  
  147. v->last=arch;
  148.  
  149. }
  150.  
  151. }
  152.  
  153.  
  154. int bfs(graphT* graph, int s, int t, int** flux, int n_vertex, stackP* stack, int* parents){
  155. int i;
  156. for(i=0;i<n_vertex;i++)
  157. parents[i]=-1;
  158. parents[s]=-2;
  159. stack_clean(stack);
  160. push(stack, &graph->vertices[s]);
  161. archT* arch;
  162. vertexT *u, *v;
  163. while(!is_empty(stack)){
  164. u= pop(stack);
  165. arch= u->arches;
  166. while(arch!=NULL){
  167. v=arch->connects;
  168. if(flux[u->id][v->id]==0 || parents[v->id]==-1){
  169. parents[v->id]=u->id;
  170. if(v->id != t)
  171. push(stack, v);
  172. else
  173. return 1;
  174. }
  175. }
  176. }
  177. return 0;
  178. }
  179.  
  180.  
  181. int edmondsKarp(graphT* graph, int s, int t, int n_vertex, stackP* stack, int** flux){
  182. int flow=0;
  183. int* parents = malloc(sizeof(int)*n_vertex);
  184. int m;
  185. vertexT *v, *u;
  186. while(1){
  187. m=bfs(graph, s, t, flux, n_vertex, stack, parents);
  188. if(!m)
  189. break;
  190. flow++;
  191. v=&graph->vertices[t];
  192. while(v->id != s){
  193. u=&graph->vertices[parents[v->id]];
  194. flux[u->id][v->id]++;
  195. flux[v->id][u->id]--;
  196. v=u;
  197. }
  198. }
  199. return flow;
  200. }
  201.  
  202.  
  203.  
  204. int main(int argc, char * argv[]) {
  205.  
  206. int n_vertex, n_arch, h;
  207.  
  208. stackP* stack=malloc(sizeof(stackP*));
  209.  
  210. graphT* graph=malloc(sizeof(graphT*));
  211. int** flux=NULL;
  212.  
  213.  
  214.  
  215.  
  216. if(scanf("%d %d\n", &n_vertex, &n_arch) ==0)
  217.  
  218. return 0;
  219.  
  220.  
  221.  
  222. flux_init(flux, n_vertex);
  223.  
  224. graph_init(graph, n_vertex);
  225.  
  226. stack_init(stack, n_vertex);
  227.  
  228.  
  229.  
  230. int i, x, y;
  231.  
  232. for(i=0;i<n_arch;i++){
  233.  
  234.  
  235.  
  236. if(scanf("%d %d\n", &x, &y)==0)
  237.  
  238. return 0;
  239.  
  240.  
  241.  
  242. add_adjacency(graph, x, y);
  243. add_adjacency(graph, y, x);
  244.  
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. return 0;
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement