-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_graph_cycle_min.c
111 lines (93 loc) · 2.17 KB
/
demo_graph_cycle_min.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include<cstdio>
#include<stdlib.h>
#include<limits>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<climits>
#include<float.h>
using namespace std;
int dp[100][100];
struct Edge
{
int src, dest, weight;
};
int max(int x,int y)
{
return ((x>y)?x:y);
}
int min(int x,int y)
{
return ((x<y)?x:y);
}
struct Graph
{
int V, E;
struct Edge* edge;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) );
graph->V = V;
graph->E = E;
graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
return graph;
}
// minimum mean cycle(allow negative weight)
void mmc(struct Graph *graph,int m)
{
int n=graph->V;
int E=E;
//cout<<
for (int i = 0; i < n; ++i)
{
memset(dp[i + 1], 0x7f, sizeof(dp[i + 1]));
for (int j = 0; j < m; ++j)
{
int u = graph->edge[j].src, v = graph->edge[j].des, w = graph->edge[j].weight;
if (dp[i][u] != INT_MAX) dp[i + 1][v] = min(dp[i + 1][v], dp[i][u] + w);
}
}
double res = DBL_MAX;
for (int i = 1; i <= n; ++i) {
if (dp[n][i] == INT_MAX) continue;
double value = -DBL_MAX;
for (int j = 0; j < n; ++j) {
value = max(value, (double)(dp[n][i] - dp[j][i]) / (n - j));
}
res = min(res, value);
}
if(res!=INT_MAX)
cout<<res;
else
cout<<"No cycle exits";
return ;
}
int main()
{
/* Let us create the graph given in above example */
int V = 4; // Number of vertices in graph
int E = 4; // Number of edges in graph
struct Graph* graph = createGraph(V, E);
// add edge 0-1 (or A-B in above figure)
graph->edge[1].src = 0;
graph->edge[1].dest = 1;
graph->edge[1].weight = 1;
// add edge 0-2 (or A-C in above figure)
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 1;
// add edge 1-2 (or B-C in above figure)
// add edge 1-3 (or B-D in above figure)
// add edge 4-3 (or E-D in above figure)
graph->edge[3].src = 2;
graph->edge[3].dest = 3;
graph->edge[3].weight = 1;
graph->edge[4].src = 3;
graph->edge[4].dest = 0;
graph->edge[4].weight = 1;
mmc(graph);
return 0;
}