UVA - 10986 Sending email 最短路的spfa算法
来源:互联网 发布:银行家算法安全性算法 编辑:程序博客网 时间:2024/05/20 04:13
题目大意:有n个服务器,m条线将其连接起来了,要求将信息从s服务器送到t服务器,求最短的时间,如果送不到的话,另外输出
解题思路:这题的数量比较大,用邻接矩阵的话会暴,所以用邻接表来表示,具体思路不变
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define maxn 100005#define INF 0x3f3f3f3fint n,m,s,t;int head[maxn],next[maxn],u[maxn],v[maxn],w[maxn],d[maxn];int vis[maxn];void init() {scanf("%d%d%d%d",&n,&m,&s,&t);memset(head,-1,sizeof(head));for(int e = 1; e <= m; e++) {scanf("%d%d%d",&u[e],&v[e],&w[e]);u[e+m] = v[e];v[e+m] = u[e];w[e+m] = w[e];next[e] = head[u[e]];head[u[e]] = e;next[e+m] = head[u[e+m]];head[u[e+m]] = e+m;}}void spfa(int scr) {memset(vis,0,sizeof(vis));for(int i = 0; i < n; i++)d[i] = INF;queue<int> q;d[scr] = 0;q.push(scr);int temp ;while(!q.empty()){temp = q.front();q.pop();vis[temp] = 0;for(int e = head[temp]; e != -1; e= next[e])if(d[temp] + w[e] < d[v[e]]) {d[v[e]] = d[temp] + w[e];if(!vis[v[e]]) {vis[v[e]] = 1;q.push(v[e]);}}}}int main() {int test,mark = 1;scanf("%d",&test);while(test--) {init();spfa(s);if(d[t] != INF)printf("Case #%d: %d\n",mark++,d[t]);elseprintf("Case #%d: unreachable\n",mark++);}return 0;}
0 0
- UVA - 10986 Sending email 最短路的spfa算法
- uva 10986 - Sending email(最短路Dijkstra)
- UVA 10986 Sending email(SPFA)
- UVA 10986 Sending email SPFA
- UVa 10986 Sending email (最短路+Dijkstra队列优化)
- UVA 10986 - Sending email(最短路 优先队列的优化+Bellman-Ford)
- UVa 10986 - Sending email (Dijkstra优化, SPFA)
- uva 10986 - Sending email
- UVA 10986 - Sending email
- UVa:10986 Sending email
- UVA - 10986 Sending email
- UVa 10986 - Sending email
- UVa 10986 - Sending email
- uva 10986 (队列优化的spfa最短路)
- 最短路算法的SPFA算法
- 最短路--SPFA算法
- 最短路SPFA算法
- 最短路: SPFA算法
- flex计算器
- Object-C加强 Protocol协议
- 进程间通信(IPC)
- linux goahead2.1.8移植
- HDU 5137 How Many Maos Does the Guanxi Worth(弗罗伊德)
- UVA - 10986 Sending email 最短路的spfa算法
- 文件的打开并输出
- 让照片导入苹果iphone手机相机胶卷,不用在电脑删除,直接在手机上删除照片图文教程
- Javadoc 执行后报告java.lang.NullPointerException问题求解
- 好文转载Apache Maven 入门篇(下)
- QTP自动化测试
- "><img src=1 onerror=alert(document.cookie)>
- LeetCode OJ 之 Intersection of Two Linked Lists (两个链表的交点)
- android HttpURLConnection