int64_t maxflow(int64_t s, int64_t t){ int64_t n = es.size(); int64_t flow = 0; vector<vector<int64_t> > me(0); //To find and create backedges in residual graph for(int i = 0; i < n ; i++){ me.push_back(vector<int64_t> (es[i].size(), -1)); } while(true){ dijkstras(s); if(dist[t] == 0) break; flow += dist[t]; int64_t cur = t; while(cur != s){ int64_t f = pred[cur].first; int64_t j = pred[cur].second; ec[f][j] -= dist[t]; if(me[f][j] == -1){ me[f][j] = es[cur].size(); me[cur].push_back(j); es[cur].push_back(f); ec[cur].push_back(dist[t]); } else { ec[cur][me[f][j]] += dist[t]; } cur = f; } } return flow; }
int main() { int i, j, src; printf("Size of the graph : "); scanf("%d", &n); cost = calloc(n, sizeof(int *)); for (i = 0; i < n; i++) { cost[i] = calloc(n, sizeof(int)); } visited = calloc(n, sizeof(int)); dist = calloc(n, sizeof(int)); printf("Enter the elements : \n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &cost[i][j]); } } printf("Source vertex : "); scanf("%d", &src); dijkstras(src); printf("Vertex\tDistance From Source\n"); for (i = 0; i < n; i++) { printf("%d\t%d\n", i, dist[i]); } return 0; }
pii maxflow(int64_t s, int64_t t){ int64_t n = es.size(); int64_t flow = 0, cost = 0; pot.assign(n,0); vector<vector<int64_t> > me(0); for(int i = 0; i < n; i++){ me.push_back(vector<int64_t> (es[i].size(), -1)); } while(true){ dijkstras(s); if(dist[t] == INF) break; //find maxadd int64_t maxadd = INF; int64_t cur = t; while(cur != s){ maxadd = min(maxadd, ecap[pred[cur].first][pred[cur].second]); cur = pred[cur].first; } cost += (pot[t] + dist[t]) * maxadd; flow += maxadd; //Potential adjust for(int i = 0; i < n; i++){ for(int j = 0; j < es[i].size(); j++){ ecost[i][j] += dist[i] - dist[es[i][j]]; } pot[i] += dist[i]; } //adjust edges cur = t; while(cur != s){ int64_t f = pred[cur].first, j = pred[cur].second; ecap[f][j] -= maxadd; if(me[f][j] == -1){ me[f][j] = es[cur].size(); me[cur].push_back(j); es[cur].push_back(f); ecost[cur].push_back(0); ecap[cur].push_back(maxadd); } else { ecap[cur][me[f][j]] += maxadd; } cur = f; } } return {flow,cost}; }
int main(int argc, char *argv[]) { int items,size, i,j,n; char *elements; /* if (argc != 2) { printf("Invalid no of Arguments\n"); exit(1); }*/ FILE *fp; char *line = NULL; size_t lineSize = 0; if(argc!=2) fp=fopen("matrix_input.txt","r"); else fp=fopen(argv[1], "r"); if (fp == NULL) { printf("Could not open file "); exit(1); } //Get count of items from file int count=1; //Get all Weights from File if((n = getline(&line, &lineSize, fp)) > 0) { i=0; if (line[n - 1] == '\n') line[n - 1] = '\0'; elements = strtok(line, ", "); elements = strtok(NULL, ", "); while(elements != NULL) { count++; elements = strtok(NULL, ", "); } } fclose(fp); int matrix [count][count]; if(argc!=2) fp=fopen("matrix_input.txt","r"); else fp=fopen(argv[1], "r"); if (fp == NULL) { printf("Could not open file"); exit(1); } for(j=0;j<count;j++) { if((n = getline(&line, &lineSize, fp)) > 0) { i=0; if (line[n - 1] == '\n') line[n - 1] = '\0'; elements = strtok(line, ", "); matrix[j][i++]=atoi(elements); elements = strtok(NULL, ", "); while(elements != NULL) { matrix[j][i++]=atoi(elements); elements = strtok(NULL, ", "); } } } fclose(fp); /*printf("count= %d\n",count); for(i=0;i<count;i++) { for(j=0;j<count;j++) printf("%d ",matrix[i][j]); printf("\n"); }*/ dijkstras(count,matrix); return 0; }