hdu 1874 畅通工程(spfa 邻接矩阵 邻接表)
题目链接
畅通工程,可以用dijkstra算法实现。
听说spfa很好用,来水一发
邻接矩阵实现:
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(); \ cout.tie(); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; ; const ll mod = 1000000007LL; + ; int mp[N][N], mp1[N][N]; int dis[N], vis[N]; int V, E; void spfa(int start) { ; i <= V; i++) dis[i] = inf; dis[start] = ; vis[start] = ; queue<int> q; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); vis[v] = ; ; i <= mp1[v][]; i++) { if (dis[mp1[v][i]] > dis[v] + mp[v][mp1[v][i]]) { dis[mp1[v][i]] = dis[v] + mp[v][mp1[v][i]]; ) q.push(mp1[v][i]), vis[mp1[v][i]] = ; } } } } int main() { int s, e, value; while (cin >> V >> E) { mem(mp,); mem(mp1,); mem(vis,); _for(i, , E) { cin >> s >> e >> value; && mp[s][e] < value) continue; mp1[s][]++; mp1[s][mp1[s][]] = e; mp[s][e] = value; //记录mp度并且记录每个度的点的下标。 mp1[e][]++; mp1[e][mp1[e][]] = s; mp[e][s] = value; } int start, _end; cin >> start >> _end; spfa(start); if (dis[_end] < inf) cout << dis[_end] << endl; else cout << "-1" << endl; } ; }
邻接表实现:
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(); \ cout.tie(); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; ; const ll mod = 1000000007LL; + ; << ; int mp[N][N], mp1[N][N]; int dis[N], vis[N]; int V, E; int first[N]; ; struct edge { int point, next, value; } e[MAXN]; void add(int u, int v, int w) { e[num].point = v; e[num].next = first[u]; e[num].value = w; first[u] = num++; } void spfa(int start) { ; i <= V; i++) dis[i] = inf; dis[start] = ; vis[start] = ; queue<int> q; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); vis[v] = ; ; tmp = e[tmp].next) { if (dis[e[tmp].point] > dis[v] + e[tmp].value) { dis[e[tmp].point] = dis[v] + e[tmp].value; , q.push(e[tmp].point); } } } } int main() { int s, e, value; while (cin >> V >> E) { num = ; mem(vis, ); int u, v, w; _for(i, , V) { first[i] = -; } _for(i, , E) { cin >> u >> v >> w; add(u, v, w); add(v, u, w); } int start, _end; cin >> start >> _end; spfa(start); if (dis[_end] < inf) cout << dis[_end] << endl; else cout << "-1" << endl; } ; }
vector存储:
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(); \ cout.tie(); using namespace std; typedef long long ll; const ll INFL = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f; ; const ll mod = 1000000007LL; + ; << ; int dis[N], vis[N]; int V, E; ; struct edge{ int point, value; edge() {} edge(int _p,int _v) {point = _p, value = _v;} }; vector <edge> e[N]; void spfa(int st) { ; i <= V; i++) dis[i] = INF; dis[st] = ; vis[st] = ; queue<int> q; q.push(st); while (!q.empty()) { int v = q.front(); q.pop(); vis[v] = ; vector <edge> :: iterator itor = e[v].begin(); for( ; itor != e[v].end(); itor++) { if(dis[(*itor).point] > dis[v] + (*itor).value) { dis[(*itor).point] = dis[v] + (*itor).value; , q.push((*itor).point); } } /*for(int i = 0; i < e[v].size(); i++) { if(dis[e[v][i].point] > dis[v] + e[v][i].value) { dis[e[v][i].point] = dis[v] + e[v][i].value; if(!vis[e[v][i].point]) vis[e[v][i].point] = 1, q.push(e[v][i].point); } }*/ } } int main() { while (~scanf("%d%d",&V,&E)) { num = ; mem(vis, ); int u, v, w; _for(i, , V) { e[i].clear(); } _for(i, , E) { scanf("%d%d%d",&u,&v,&w); e[u].push_back(edge(v,w)); e[v].push_back(edge(u,w)); } int st, ed; cin >> st >> ed; spfa(st); if (dis[ed] < INF) cout << dis[ed] << endl; else cout << "-1" << endl; } ; }
hdu 1874 畅通工程(spfa 邻接矩阵 邻接表)的更多相关文章
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
- HDU 1874 畅通工程续 SPFA || dijkstra||floyd
http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 给你一些点,让你求S到T的最短路径. 我只是来练习一下SPFA的 dijkstra+邻接矩阵 ...
- HDU——1874畅通工程续(邻接矩阵弗洛伊德)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU——1874畅通工程续(Dijkstra与SPFA)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
随机推荐
- HDU 4313树形DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Qt ---------- connect连接类型
Qt::AutoConnection 0 (Default) If the receiver lives in the thread that emits the signal, Qt::Direct ...
- sql 建表以及查询---复杂查询之成绩排名
废话不说,直接建表 1.表Player USE T4st -- 设置当前数据库为T4st,以便访问sysobjects IF EXISTS(SELECT * FROM sysobjects WHERE ...
- transition和animation概况
有人可能会有疑问,CSS3动画不是只有animation一个属性吗?怎么又和转化(transform)和过渡(transition)扯上关系了,其实并非如此,转化(transform)属性让动画的变换 ...
- linux用户管理和文件权限
linux用户管理和文件权限 新建用户:useradd ftpuser useradd -g gxx userxx修改密码:passwd ftpuser新增用户组:# groupadd gr ...
- PAT (Advanced level) 1003. Emergency (25) Dijkstra
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- SVN服务器更换IP,客户端重新定位
svn服务器更换ip,后客户端需要重新定位,操作如下: 1.找到你的项目文件所在的根目录,右键点击空白地方,弹出菜单 TortoiseSVN-->Relocate 点击Relocate ,弹出重 ...
- java collection(一)
1.Collection层次结构: 2.集合Conllection的基本概念: (1)集合的基本认识:如StringBuffer&StringBuilder是集合(存储的对象类型是String ...
- 「6月雅礼集训 2017 Day10」perm(CodeForces 698F)
[题目大意] 给出一个$n$个数的序列$\{a_n\}$,其中有些地方的数为0,要求你把这个序列填成一个1到$n$的排列,使得: $(a_i, a_j) = 1$,当且仅当$(i, j) = 1$.多 ...
- CRB and Candies(组合数学+求逆元+lcm)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题目: Problem Description CRB has N different cand ...