标签:log span style ons from 总数 含义 book 单元
1 const int INF = 9999999999;//设一个较大的数为无穷大 2 int n, m;//n为点数,m为边数 3 int e[5005][5005];//貌似开5005*5005就快MLE了...所以要谨慎一点 4 for (int i = 1; i <= n; i++) 5 for (int j = 1; j <= n; j++) 6 if (i == j) 7 e[i][j] = 0;//我自己到我自己的距离当然是0 8 else 9 e[i][j] = INF;//一开始还没有边,所以我到其他人的距离先设为无穷大 10 for (int i = 1; i <= m; i++)//读入边 11 { 12 int from, to, weight;//从哪来,到哪去,路多长 13 cin >> from >> to >> weight; 14 e[from][to] = weight;//无向图存两遍 15 e[to][from] = weight;//from到to的距离和to到from的距离是相等的 16 }
1 #include<iostream> 2 using namespace std; 3 struct edge 4 { 5 int from; 6 int to; 7 int next;//模拟指针 8 int weight; 9 }e[2000080];//看吧,他开很大都不会爆,不过要注意无向图开两倍 10 //毕竟一条无向边其实是当作两条有向边存的 11 int head[50005];//head[i]表示点i所发出的第一条边的数组下标 12 int tot;//边的总数 13 int n, m; 14 void add(int from,int to,int weight) 15 { 16 tot++; 17 e[tot].from = from; 18 e[tot].to = to; 19 e[tot].weight = weight; 20 e[tot].next = head[from]; 21 head[from] = tot; 22 }//加边的模板 23 int main() 24 { 25 cin >> n >> m; 26 for (int i = 1; i <= m; i++) 27 { 28 int x, y, z; 29 cin >> x >> y >> z; 30 add(x, y, z); 31 add(y, x, z);//依然无向边存两次 32 } 33 for (int i = head[1]; i; i = e[i].next) 34 //遍历该点上所有的边,如果没有下一条了(i=0),我就停 35 //如果还有下一条边,我就继续往后遍历(i=e[i].next) 36 cout << e[i].to; 37 //貌似没解释清楚,感性理解一下? 38 return 0; 39 }
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct edge { int from; int to; int weight; }; vector<edge> e[100086];//e[i][j]表示点i的第j条边 //貌似比邻接表稍微简单一点 int n, m; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { edge t;//定义一条新的的边出来 int x, y, z; cin >> x >> y >> z; t.from = x; t.to = y; t.weight; e[x].push_back(t);//把他塞进去 t.from = y; t.to = x; e[y].push_back(t);//改一改,反向塞进去 } for (int i = 0; i < e[1].size(); i++) //查询很方便 //不过注意vector是从0开始的 cout << e[1][i].to; return 0; }
1 for (int k = 1; k <= n; k++)//枚举中间点 2 for (int i = 1; i <= n; i++)//枚举起点 3 for (int j = 1; j <= n; j++)//枚举终点 4 e[i][j] = min(e[i][j], e[i][k] + e[k][j]);//替换
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 int e[1000][1000]; 6 int main() 7 { 8 int n, m; 9 cin >> n >> m; 10 for (int i = 1; i <= n; i++) 11 for (int j = 1; j <= m; j++) 12 if (i == j) 13 e[i][j] = 0; 14 else 15 e[i][j] = 9999999; 16 for (int i = 1; i <= m; i++) 17 { 18 int x, y, z; 19 cin >> x >> y >> z; 20 e[x][y] = z; 21 e[y][z] = z; 22 } 23 for (int k = 1; k <= n; k++)//枚举中间点 24 for (int i = 1; i <= n; i++)//枚举起点 25 for (int j = 1; j <= n; j++)//枚举终点 26 e[i][j] = min(e[i][j], e[i][k] + e[k][j]);//替换 27 return 0; 28 }
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const long inf = 20041001; 5 int n; 6 int m; 7 int s; 8 int tot; 9 struct edge 10 { 11 long weight; 12 int to; 13 int next; 14 }e[500005]; 15 struct node 16 { 17 int head; 18 }no[10086]; 19 long long dis[10086]; 20 bool book[10086]; 21 void add(int from, int to, int weight) 22 { 23 tot++; 24 e[tot].to = to; 25 e[tot].weight = weight; 26 e[tot].next = no[from].head; 27 no[from].head = tot; 28 } 29 int main() 30 { 31 cin >> n >> m >> s; 32 book[s] = 1; 33 for (int i = 1; i <= n; i++) 34 dis[i] = inf; 35 dis[s] = 0; 36 for (int i = 1; i <= m; i++) 37 { 38 int x, y, w; 39 cin >> x >> y >> w; 40 if (x != y) 41 add(x, y, w); 42 } 43 for (int i = no[s].head; i; i = e[i].next) 44 { 45 if (e[i].weight < dis[e[i].to]) 46 dis[e[i].to] = e[i].weight; 47 } 48 for (int i = 1; i < n; i++) 49 { 50 int u = 0; 51 int minn = inf; 52 for (int j = 1; j <= n; j++) 53 { 54 if (book[j] == 0 && dis[j] < minn) 55 { 56 minn = dis[j]; 57 u = j; 58 } 59 } 60 book[u] = 1; 61 for (int i = no[u].head; i; i = e[i].next) 62 { 63 if (dis[e[i].to] > dis[u] + e[i].weight) 64 dis[e[i].to] = dis[u] + e[i].weight; 65 } 66 } 67 for (int i = 1; i <= n; i++) 68 cout << dis[i] << " "; 69 return 0; 70 }
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #include<vector> 5 #include<map> 6 #include<bitset> 7 #include<set> 8 #include<string> 9 #if !defined(_WIN32) 10 #include<bits/stdc++.h> 11 #endif // !defined(_WIN32) 12 #define ll long long 13 #define dd double 14 using namespace std; 15 int t; 16 int n, m, w; 17 int tot; 18 struct edge 19 { 20 int from; 21 int to; 22 int weight; 23 }e[100086]; 24 int dis[5005]; 25 void add(int x, int y, int z) 26 { 27 tot++; 28 e[tot].from = x; 29 e[tot].to = y; 30 e[tot].weight = z; 31 } 32 bool Bellman_Ford() 33 { 34 memset(dis, 0x3f3f3f3f, sizeof(dis)); 35 dis[1] = 0; 36 for (int i = 1; i < n; i++) 37 { 38 for (int j = 1; j <= tot; j++) 39 { 40 if (dis[e[j].to] > dis[e[j].from] + e[j].weight) 41 dis[e[j].to] = dis[e[j].from] + e[j].weight; 42 } 43 } 44 for (int i = 1; i <= tot; i++) 45 if (dis[e[i].to] > dis[e[i].from] + e[i].weight) 46 return 0; 47 return 1; 48 } 49 int main() 50 { 51 cin >> t; 52 while (t--) 53 { 54 tot = 0; 55 memset(e, 0, sizeof(e)); 56 memset(dis, 0, sizeof(dis)); 57 n = 0, m = 0, w = 0; 58 cin >> n >> m >> w; 59 for (int i = 1; i <= m; i++) 60 { 61 int x, y, z; 62 cin >> x >> y >> z; 63 add(x, y, z); 64 add(y, x, z); 65 } 66 for (int i = 1; i <= w; i++) 67 { 68 int x, y, z; 69 cin >> x >> y >> z; 70 add(x, y, 0 - z); 71 } 72 if (Bellman_Ford()) 73 cout << "NO" << endl; 74 else 75 cout << "YES" << endl; 76 } 77 return 0; 78 }
标签:log span style ons from 总数 含义 book 单元
原文地址:https://www.cnblogs.com/HNFOX/p/11272427.html