HD1874 畅通工程续
来源:互联网 发布:python 3.5.2打开 编辑:程序博客网 时间:2024/06/15 01:38
思考:最短路入门题目,可以用求最短路的算法,数据量也不大,比如Dijkstra, floy 或者SPFA算法。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;const int maxn = 210;const int maxm = 1100;const int INF = 0x7fffffff;int gn, gm;bool inque[maxn];int dist[maxn];queue<int> Q;vector<pair<int, int> > g[maxn + 10];void spfa(const int s, const int t) { int i; memset(inque, false, sizeof(inque)); for(i = 0; i < gn; i++) dist[i] = INF; dist[s] = 0; while(!Q.empty()) Q.pop();//清空队列. Q.push(s); inque[s] = true; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(i = 0; i < (int)g[u].size(); i++) { int t = g[u][i].first; if(dist[u] != INF && g[u][i].second != INF && dist[u]+g[u][i].second < dist[t]) { dist[t] = dist[u] + g[u][i].second; if(!inque[t]) { inque[t] = true; Q.push(t); } } } inque[u] = false; } if(dist[t] != INF) { printf("%d\n", dist[t]); } else printf("-1\n");}void init() { int i; for(i = 0; i < maxn; i++) { g[i].clear(); }}int main(){ int i; int x, y, w; pair<int,int> t; while(scanf("%d%d", &gn, &gm) != EOF) { init();//清空容器. for(i = 0; i < gm; i++) { scanf("%d%d%d", &x, &y, &w); t.first = y; t.second = w; g[x].push_back(t); t.first = x; g[y].push_back(t); } int s, tt; scanf("%d%d", &s, &tt); spfa(s, tt); } return 0;}
0 1
- HD1874 畅通工程续
- hd1874畅通工程续
- hdu1874畅通工程续
- 畅通工程续
- HDOJ 畅通工程续
- hdu1874 畅通工程续
- HDU1874 畅通工程续
- hdoj1874 畅通工程续
- HDU1874 畅通工程续
- 畅通工程续
- 畅通工程续
- 畅通工程续
- hdu1874 畅通工程续
- HDU1874:畅通工程续
- hdu1874畅通工程续
- hdu_1874 畅通工程续
- hdu1874畅通工程续
- hdu1874畅通工程续
- JQuery高性能最佳实践
- mmap函数和write函数(多了一次拷贝)--inux函数
- 带色彩恢复的多尺度视网膜增强算法(MSRCR)的原理、实现及应用。
- html:image具有自动提交功能
- Struts2中访问web元素的四种方式
- HD1874 畅通工程续
- 在hadoop启动的时候,会出现各种各样的问题
- Windows二进制文件合并工具
- 马云给北大学子讲创业:阿里为什么一直不提供班车和宿舍
- OK210烧写WINCE系统时提示SD Init Error
- java Object 的equals方法的复写
- 奥巴马终表态:全力协助
- 将Java异常信息保存到字符串中
- Update one table with the data in another table