标签:turn for name algo names pac string color code
这题用floyd和dijkstra都可以写,但是在用dijkstra时,如果将起始点的vis[a]标记为true,就会WA,但是在啊哈算法里面,是要提前标记的,我不知道为什么TUT,还在思索中
另外学到了一个新的表示方式
const int inf=0x3f3f3f3f;
memset(dis,0x3f,sizeof(dis));
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int inf = 0x3f3f3f3f; 6 const int N = 110; 7 int e[N][N], dis[N]; 8 bool vis[N]; 9 int n; 10 void dijkstra(int a){ 11 memset(dis, 0x3f, sizeof(dis)); 12 dis[a] = 0; 13 //vis[a] = true; 14 for (int i = 1; i <= n-1;i++){ 15 int t = 0; 16 int minn = inf; 17 for (int j = 1; j <= n;j++){ 18 if(!vis[j]&&dis[j]<minn){ 19 minn = dis[j]; 20 t = j; 21 } 22 } 23 vis[t] = true; 24 for (int j = 1; j <= n;j++){ 25 dis[j] = min(dis[t] + e[t][j],dis[j]); 26 } 27 } 28 } 29 void floyd(){ 30 int i, j, k; 31 for (k = 1; k <= n;k++) 32 for (i = 1; i <= n;i++) 33 for (j = 1; j <= n;j++) 34 e[i][j] = min(e[i][k] + e[k][j],e[i][j]); 35 } 36 int main(){ 37 int a, b; 38 cin >> n >> a >> b; 39 for (int i = 0; i <= n;i++){ 40 for (int j = 0; j <= n;j++){ 41 if(i==j) 42 e[i][j] = 0; 43 else 44 e[i][j] = inf; 45 } 46 } 47 for (int i = 1; i <= n;i++){ 48 int m; 49 cin >> m; 50 for (int j = 1; j <= m;j++){ 51 int x; 52 cin >> x; 53 if(j==1) 54 e[i][x] = 0; 55 else 56 e[i][x] = 1; 57 } 58 } 59 60 dijkstra(a); 61 if(dis[b]==inf) 62 cout << -1 << endl; 63 else 64 cout << dis[b] << endl; 65 66 67 //方法2 68 floyd(); 69 if(e[a][b]>=inf) 70 cout << -1 << endl; 71 else 72 cout << e[a][b] << endl; 73 74 system("pause"); 75 return 0; 76 }
pair和memset的头文件是cstring,在POJ里不能用万能头,所以要清楚头文件是什么
标签:turn for name algo names pac string color code
原文地址:https://www.cnblogs.com/guaguastandup/p/10415975.html