#include #include #define SIZE 8 #define INF 99999 typedef struct queue { int front; int rear; int *arr; } queue; queue *newQueue() { queue *q = malloc(sizeof(queue)); q->arr = malloc(sizeof(int) * SIZE * 30); // make this larger q->front = 0; q->rear = 0; return q; } void enqueue(queue *q, int i) { q->arr[q->front++] = i; } int deque(queue *q) { return q->arr[q->rear++]; } int is_empty(queue* q) { return (q->front == q->rear); } int min(int l, int r) { return (l > r) ? r : l; } int max(int l, int r) { return (l > r) ? l : r; } int **newMatrix() { int **mat = malloc(sizeof(int *) * SIZE); for (int i = 0; i < SIZE; i++) { mat[i] = malloc(sizeof(int) * SIZE); for (int j = 0; j < SIZE; j++) { mat[i][j] = INF; // make sure every point is not reachable by default } } mat[1][2] = 3; mat[1][3] = 3; mat[1][4] = 6; mat[2][4] = 2; mat[2][5] = 5; mat[3][4] = 3; mat[3][6] = 3; mat[4][5] = 2; mat[4][6] = 2; mat[4][7] = 5; mat[5][7] = 3; mat[6][7] = 4; return mat; } void getec(int *ec, int **mat) { int cur; queue *q = newQueue(); for (int i = 1; i < SIZE; i++) { // eneueue anything that has in-degree == 0 int is_start = 1; for (int j = 1; j < SIZE; j++) { if (mat[j][i] != INF) { is_start = 0; break; } } if (is_start == 1) { ec[i] = 0; enqueue(q, i); } } while (!is_empty(q)) { cur = deque(q); for (int j = 1; j < SIZE; j++) { if (mat[cur][j] != INF) { ec[j] = max(ec[j], ec[cur] + mat[cur][j]); enqueue(q, j); } } } } void getlc(int *lc, int* ec, int **mat) { int cur; queue *q = newQueue(); for (int i = 1; i < SIZE; i++) { // enqueue any point that is an end int is_end = 1; for (int j = 1; j < SIZE; j++) { if (mat[i][j] != INF) { is_end = 0; break; } } if (is_end == 1) { lc[i] = ec[i]; enqueue(q, i); } } while (!is_empty(q)) { cur = deque(q); for (int j = 1; j < SIZE; j++) { if (mat[j][cur] != INF) { lc[j] = min(lc[j], lc[cur] - mat[j][cur]); enqueue(q, j); } } } } int main(void) { int **mat = newMatrix(); int start, end, node; scanf("%d,%d,%d,", &start, &end, &node); mat[start][end] = INF; int ec[SIZE] = {}; int lc[SIZE]; for (int i = 1; i < SIZE; i++) { lc[i] = INF; } // start from 1 getec(ec, mat); lc[SIZE - 1] = ec[SIZE - 1]; getlc(lc, ec, mat); printf("%d", lc[node] - ec[node]); // for (int i = 1; i < SIZE; i++) { // printf("%d: %d - %d = %d\n", i, lc[i], ec[i], lc[i] - ec[i]); // } return 0; }