Hdu_1874 continuous Engineering (spfa)

Source: Internet
Author: User

When I checked the spfa information, I occasionally saw a kind of application, and then I knocked on 1A very calmly.

15 ms + 328 K code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 207;
const int inf = 100000000;

int vis[N], map[N][N];
int dis[N], q[N];

void spfa(int n, int s)
{
int i, f, r, p;
memset(vis, 0, sizeof(vis));
for(i = 0; i < N; i++)
q[i] = 0;
for(i = 0; i < n; i++)
dis[i] = inf;
dis[s] = 0; vis[s] = 1;
q[0] = s; f = 0; r = 1;
while(f < r)
{
p = q[f];
for(i = 0; i < n; i++)
if(dis[i] > map[p][i] + dis[p])
{
dis[i] = map[p][i] + dis[p];
if(!vis[i])
{
q[r++] = i;
vis[i] = 1;
}
}
vis[p] = 0;
f++;
}
}

int main()
{
int n, m, i, j, a, b, c;
//freopen("data.in", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
map[i][j] = inf;
if(i == j) map[i][j] = 0;
}
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
if(c < map[a][b])
map[a][b] = map[b][a] = c;
}
scanf("%d%d", &a, &b);
spfa(n, a);
if(dis[b] < inf)
printf("%d\n", dis[b]);
else
printf("-1\n");
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.