這個算是圖論裏面的簡單題了,可以用floyd,dijkstra,bfs去做,,只當練手。。
floyd做。。
#include<stdio.h> #include<string.h> #include<stdlib.h> int mat[21][21]; const int INF = 99999; int main(void) { int n,from,to,t; int i,j,k,start=1; while( scanf("%d",&n) != EOF ) { for( i = 1; i <= 20; i++ ) for( j = 1; j <= 20; j++ ) mat[i][j] = INF; while( n-- ) { scanf("%d",&to); mat[1][to] = 1; mat[to][1] = 1; } for( i = 2;i <= 19;i++) { scanf("%d",&n); while( n-- ) { scanf("%d",&to); mat[i][to] = 1; mat[to][i] = 1; } } for( k = 1; k <= 20; k++ ) for( i = 1; i <= 20; i++ ) for( j = 1; j <= 20; j++ ) if( mat[i][j] > mat[i][k] + mat[k][j] ) mat[i][j] = mat[i][k] + mat[k][j]; scanf("%d",&n); printf("Test Set #%d/n",start++); while( n-- ) { scanf("%d%d",&from,&to); printf("%d to %d: %d/n",from,to,mat[from][to]); } printf("/n"); } return 0; }
用dijkstra:
#include<stdio.h> #include<string.h> #include<stdlib.h> int mat[21][21]; int flag[21]; int dist[21]; const int INF = 99999999; int main(void) { int n,from,to,min,now; int i,j,k,start=1; while( scanf("%d",&n) != EOF ) { for( i = 1; i <= 20; i++ ) for( j = 1; j <= 20; j++ ) mat[i][j] = INF; while( n-- ) { scanf("%d",&to); mat[1][to] = 1; mat[to][1] = 1; } for( i = 2;i <= 19;i++) { scanf("%d",&n); while( n-- ) { scanf("%d",&to); mat[i][to] = 1; mat[to][i] = 1; } } scanf("%d",&n); printf("Test Set #%d/n",start++); while( n-- ) { scanf("%d%d",&from,&to); for( i = 1;i<=20;i++ ) dist[i]=INF; memset(flag,0,sizeof(flag)); flag[from] = 1;dist[now=from]=0; for( i = 1; i < 20; i++) { for( j = 1; j <= 20; j++ ) if( !flag[j] && dist[j] > dist[now] + mat[now][j] ) dist[j] = dist[now] + mat[now][j]; for( j = 1,min = INF; j <= 20; j++ ) if( !flag[j] && dist[j] < min ) min = dist[now=j]; flag[now]=1; } printf("%d to %d: %d/n",from,to,dist[to]); } printf("/n"); } return 0; }
bfs:
#include<stdio.h> #include<string.h> #include<stdlib.h> int mat[21][21]; int q[100],flag[21],dist[21]; int front=0,back=0; const int INF = 99999999; int main(void) { int n,from,to; int i,j,t,start=1; while( scanf("%d",&n) != EOF ) { for( i = 1; i <= 20; i++ ) for( j = 1; j <= 20; j++ ) mat[i][j] = INF; while( n-- ) { scanf("%d",&to); mat[1][to] = 1; mat[to][1] = 1; } for( i = 2;i <= 19;i++) { scanf("%d",&n); while( n-- ) { scanf("%d",&to); mat[i][to] = 1; mat[to][i] = 1; } } scanf("%d",&n); printf("Test Set #%d/n",start++); while( n-- ) { scanf("%d%d",&from,&to); front = back = 0; for( i = 1;i<=20;i++ ) flag[i] = 0; q[back++] = from; flag[from] = 1; dist[from] = 0; while( back!=front ) { t = q[front++]; for( i = 1;i <=20;i++) if( !flag[i] && mat[t][i] == 1 ) { dist[i] = dist[t] + 1; flag[i] = 1; q[back++] = i; } } printf("%d to %d: %d/n",from,to,dist[to]); } printf("/n"); } return 0; }
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on
鏈接 https://leetcode-cn.com/problems/wildcard-matching/ 耗時 解題:4.5 h 題解:36 min 題意 給定一個字符串(s)和一個字符模式(p) ,實現一個支持 ‘?’ 和
廣度優先搜索(BFS)的一個常見應用是找出從根結點到目標結點的最短路徑。 結點的處理順序是什麼? 與樹的層序遍歷類似,越是接近根結點的結點將越早地遍歷。 如果在第 k 輪中將結點 X 添加到隊列中,則根結點與 X 之間的最短路徑
題目描述 隨着新鐵路線的貫通,夏之國的商貿日益繁榮。看着一輛輛滿載貨物的列車駛入車站,前來搬運貨物的工人們排成了長龍。在這炎炎烈日下,他們被僱傭着,要搬運貨物到指定的位置。 如下圖所示,夏之國所在區域可以看成是一個n×n的方格圖,
題目描述: 給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 輸入: [3,9,20,null,null,15,7] 輸出: [ [3], [20,9], [
題意很簡單,寫兩個優先隊列的bfs即可,dis小的排在前面即可,第二個bfs開一個變量記錄每次從當前位置轉移到下一個位置的方向即可。 #include<iostream> #include<cstdio> #include<cstri
文章目錄原題鏈接: 原題鏈接: https://ac.nowcoder.com/acm/contest/549/G 是個裸的雙向BFS的題,看到一份比較好的代碼,我以前好像都是寫的兩個BFS的,而他這個寫在一起了,而且更好得體現了
題目: Input An edge-weighted graph G (V, E). |V| |E| s0 t0 d0 s1 t1 d1 : s|E|-1 t|E|-1 d|E|-1 |V| is the number of ve
最近在做最短路徑的題目,有幾道題是同一個類型的,題意基本都是尋找從A到B所有路徑中,路徑的最大邊的最小值。這樣類型的題有POJ - 2253 Frogger, UVA - 10048 Audiophobia,POJ - 1797
暢通工程續 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 41691
前言 BFS模板題 正文 【問題描述】 下圖給出了一個迷宮的平面圖,其中標記爲 1 的爲障礙,標記爲 0 的爲可以通行的地方。 010000 000100 001001 110000 迷宮的入口爲左上角,出口爲右下角,在迷宮
A - Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much sleep as possib
題目鏈接 題意:在一個蜂巢裏,一隻小蜜蜂要從 S 點飛到 T 點。 題解:題目已經保證蜂巢的結構一定是樣例給定的框架,那麼我們可以給按照規律從左到右,從上到下給每個六邊形編號。再用bfs連邊,最後跑一遍最短路即可,思路簡單,只是連邊過程有
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1395
附上詳解博客 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 代碼是學長敲的,我在不懂的地方加了許多註釋,我感覺這一週的東西學的特別喫力,都是一知半解