书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

更加深入的了解SPFA

SPFA算法是求单源路径的经典算法,就是一点到其它所有点的最短路径。

这个类似于Flyod但是效率要高很多。下面给出具体的演示。

Minya Konka decided to go to Fuzhou to participate in the ACM regional contest at their own expense.Through the efforts, they got a small amount of financial support from their school, but the school could pay only one of those costs between two stations for them.

From SWUST to Fujian Normal University which is the contest organizer, there are a lot of transfer station and there are many routes.For example, you can take the bus to Mianyang Railway Station (airport), then take the train (plane) to Fuzhou,and then take a bus or taxi to the Fujian Normal University.

The school could pay only one of those costs between two stations for them, the others paid by the Minya Konka team members.They want to know what is the minimum cost the need pay.Can you calculate the minimum cost?

Input

There are several test cases.

In each case,the first line has two integers n(n<=100) and m(m<=500),it means there are n stations and m undirected roads.

The next m lines, each line has 3 integers u,v,w,it means there is a undirected road between u and v and it cost w.(1<=u,v<=n,0<=w<=100)

The ID of SWUST is 1,and n is the ID of Fujian Normal University.

Output

If they can not reach the destination output -1, otherwise output the minimum cost.

Sample Input

5 5
1 2 7
1 3 3
3 4 3
2 5 3
4 5 3
复制代码
#include "iostream"
#include "cstring"
#include "queue"
using namespace std;
#define INF 999999999
#define eMax 505
#define nMax 105
bool vis[nMax];
int dis[2][nMax], num, rec[nMax], n, m;
struct Node
{
int u, v, w, pre;
}e[eMax*2];
void Init(int u, int v, int w)
{
e[num].u = u;
e[num].v = v;
e[num].w = w;
e[num].pre = rec[u];
rec[u] = num++;
}
void spfa(int s, int p)
{
memset(vis, false, sizeof(vis));
dis[p][s] = 0;
queue<int> q;
q.push(s);
while(!q.empty())
{
s = q.front();
q.pop();
vis[s] = false;
int j, v;
for(j=rec[s]; j!=-1; j=e[j].pre)
{
v = e[j].v;
if(dis[p][s] + e[j].w < dis[p][v])
{
dis[p][v]=dis[p][s]+e[j].w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
}
int main()
{
int u, v, w;
while(cin>>n>>m)
{
num = 0;
memset(rec, -1, sizeof(rec));
memset(dis, 0x3f, sizeof(dis));
int ans = INF;
while(m--)
{
cin>>u>>v>>w;
Init(u, v, w);
Init(v, u, w);
}
spfa(1, 0);
spfa(n, 1);
for(int i=0; i<num; i++)
if(dis[0][e[i].u]+dis[1][e[i].v] < ans)
ans = dis[0][e[i].u]+dis[1][e[i].v];
if(ans < INF) cout<<ans<<endl;
else cout<<"-1"<<endl;
}
}
复制代码
在上面的代码中我们用了一个二维的数组来分别保留
1到其它所有点的最短距离和n到其它所有点的最短距离。
就是这样,这个就是SPFA。

posted on   More study needed.  阅读(366)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10

导航

统计

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

点击右上角即可分享
微信分享提示