1. Trang chủ
  2. » Kỹ Năng Mềm

Thuyền trưởng haddock

21 3 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Thuyền Trưởng Haddock
Định dạng
Số trang 21
Dung lượng 41,1 KB

Nội dung

tai lieu thi hoc ki ly thuyet do thi adjsjdskkdasjdjksjdkjfjjfjfjfjfjfjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjlsdlas;dls;dls;ld;sald;ls;dlsa;dl;sald;sld;sald;lsa;dl;sdl;sld;l;l;djsbdbsdnsmdnbnsdbnsbdnsabdnabsdnbdnasbdnasbdnsbdnbsamndbmsadbmnsbdnbdnasbdnsadbmsadbnsdbnbasmndbmsabdmnsabdnasbdnbsandbsanmdbansbdnmasb

Thuyền trưởng Haddock #include #define MAX 100 #define WHITE 0 #define GRAY 1 #define BLACK 2 typedef struct { int u, v; }Edge; typedef struct { int n, m; Edge edges[MAX]; }Graph; void init_graph(Graph* pG, int n) { pG->n = n; pG->m = 0; } void add_edge(Graph* pG, int u, int v) { pG->edges[pG->m].u = u; pG->edges[pG->m].v = v; pG->m++; } int adjacent(Graph* pG, int u, int v) { for (int e = 0; e < pG->m; e++) if ((pG->edges[e].u == u && pG->edges[e].v == v)) return 1; return 0; } int color[MAX]; int has_circle; void DFS(Graph* pG, int s, int p) { color[s] = GRAY; for (int v = 1; v n; v++) if (adjacent(pG, s, v)) { if (color[v] == WHITE) DFS(pG, v, s); else if (color[v] == GRAY) { has_circle = 1; } } color[s] = BLACK; } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 1; e edges[pG->m].u = u; pG->edges[pG->m].v = v; pG->m++; } int adjacent(Graph* pG, int u, int v) { for (int e = 0; e < pG->m; e++) if ((pG->edges[e].u == u && pG->edges[e].v == v) || (pG->edges[e].u == v && pG->edges[e].v == u)) return 1; return 0; } int parent[MAX]; int color[MAX]; int has_circle; void DFS(Graph* pG, int s, int p) { color[s] = GRAY; parent[s] = p; for (int v = 1; v n; v++) if (adjacent(pG, s, v)) { if (v == p) continue; if (color[v] == WHITE) DFS(pG, v, s); else if (color[v] == GRAY) { has_circle = 1; } } color[s] = BLACK; } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 1; e rear++; pQ->data[pQ->rear] = u; } void dequeue(queue* pQ) { pQ->front++; } elementype front(queue Q) { return Q.data[Q.front]; } int empty(queue Q) { return Q.front > Q.rear; } void init_graph(Graph* pG, int n) { pG->n = n; pG->m = 0; } void add_edge(Graph* pG, int u, int v) { pG->edges[pG->m].u = u; pG->edges[pG->m].v = v; pG->m++; } int adjacent(Graph* pG, int u, int v) { for (int e = 0; e < pG->m; e++) if ((pG->edges[e].u == u && pG->edges[e].v == v) || (pG->edges[e].u == v && pG->edges[e].v == u)) return 1; return 0; } int mark[MAX]; int parent[MAX]; void BFS(Graph* pG, int s) { queue Q; make_null_queue(&Q); elementype pair; pair.u = s; pair.p = -1; enqueue(&Q, pair); while (!empty(Q)) { elementype pair = front(Q); dequeue(&Q); int u = pair.u, p = pair.p; if (mark[u] != 0) continue; mark[u] = 1; parent[u] = p; for (int v = 1; v n; v++) if (adjacent(pG, u, v)) { elementype pair = { v, u }; enqueue(&Q, pair); } } } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 1; e top_id] = x; } void pop_back(stack* pS) { pS->top_id ; } elementtype back(stack S) { return S.data[S.top_id]; } int empty(stack S) { return S.top_id == -1; } void init_graph(Graph* pG, int n) { pG->n = n; pG->m = 0; } void add_edge(Graph* pG, int u, int v) { pG->edges[pG->m].u = u; pG->edges[pG->m].v = v; pG->m++; } int adjacent(Graph* pG, int u, int v) { for (int e = 0; e < pG->m; e++) if ((pG->edges[e].u == u && pG->edges[e].v == v) || (pG->edges[e].u == v && pG->edges[e].v == u)) return 1; return 0; } int mark[MAX]; int parent[MAX]; void DFS(Graph* pG, int s) { stack S; make_null_stack(&S); elementtype pair = { s, 0 }; push_back(&S, pair); while (!empty(S)) { elementtype pair = back(S); pop_back(&S); int u = pair.u, p = pair.p; if (mark[u] != 0) continue; mark[u] = 1; parent[u] = p; for (int v = 1; v n; v++) if (adjacent(pG, u, v)) { elementtype pair = { v, u }; push_back(&S, pair); } } } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 1; e m].u = u; pG->edges[pG->m].v = v; pG->m++; } int adjacent(Graph* pG, int u, int v) { for (int e = 0; e < pG->m; e++) if ((pG->edges[e].u == u && pG->edges[e].v == v) || (pG->edges[e].u == v && pG->edges[e].v == u)) return 1; return 0; } int mark[MAX]; int parent[MAX]; void DFS(Graph* pG, int s, int p) { mark[s] = 1; parent[s] = p; for (int v = 1; v n; v++) if (adjacent(pG, s, v) && mark[v] == 0) { DFS(pG, v, s); } } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 1; e m++; } #define oo 999999 int mark[100], p[100], pi[100]; void Dijkstra(Graph* pG, int s) { for (int u = 1; u n; u++) { mark[u] = 0; pi[u] = oo; } pi[s] = 0; p[s] = -1; int u; for (int i = 1; i < pG->n; i++) { int minpi = oo; for (int v = 1; v n; v++) { if (!mark[v] && pi[v] < minpi) { minpi = pi[v]; u = v; } } mark[u] = 1; for (int v = 1; v n; v++) { if (!mark[v] && pG->W[u][v] != NO_EDGE) { if (pi[u] + pG->W[u][v] < pi[v]) { pi[v] = pi[u] + pG->W[u][v]; p[v] = u; } } } } } int main() { int n, m; scanf("%d %d", &n, &m); Graph G; init_graph(&G, n); for (int e = 0; e < m; e++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); add_edge(&G, u, v, w); } Dijkstra(&G, 1); if (pi[n] < oo) printf("%d", pi[n]); else printf("-1"); return 0; } #Mê cung số #include #define NO_EDGE -1 typedef struct { int n, m; int W[100][100]; }Graph; void init_graph(Graph* pG, int n) { pG->n = n; pG->m = 0; for (int u = 1; u W[u][v] = w; pG->m++; } #define oo 999999 int mark[100], p[100], pi[100]; void Dijkstra(Graph* pG, int s) { for (int u = 1; u n; u++) { mark[u] = 0; pi[u] = oo; } pi[s] = 0; p[s] = -1; int u; for (int i = 1; i < pG->n; i++) { int minpi = oo; NO_EDGE) { for (int v = 1; v n; v++) { { if (!mark[v] && pi[v] < minpi) { [v]; minpi = pi[v]; u = v; } } } } mark[u] = 1; for (int v = 1; v n; v++) { if (!mark[v] && pG->W[u][v] != if (pi[u] + pG->W[u][v] < pi[v]) pi[v] = pi[u] + pG->W[u] p[v] = u; } } } int main() { int n, m; scanf("%d %d", &m, &n); Graph G; init_graph(&G, n * m); for (int i = 1; i n) { add_edge(&G, i - n, i, w); if (i + n

Ngày đăng: 14/03/2024, 19:48

w