POJ_2394_最短路径---SPFA算法
2010-06-09 18:16
323 查看
//============================================================================
// Name : POJ_2394.cpp
// Author : tiger
// Version :
// Copyright : Your copyright notice
// Description : 最短路径---SPFA算法
//============================================================================
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 501;
const int inf = 100000000;
int mat[MAXN][MAXN];
bool v[MAXN];
int dist[MAXN];
void spfa(int s,int n)
{
queue<int> q;
q.push(s);
v[s] = true;
dist[s] = 0;
int t,i;
while (!q.empty())
{
t = q.front();
q.pop();
v[t] = false;
for (i = 0; i < n; i++)
{
if (dist[t] + mat[t][i] < dist[i])
{
dist[i] = dist[t] + mat[t][i];
if (!v[i])
{
v[i] = true;
q.push(i);
}
}
}
}
}
int main()
{
freopen("in", "r", stdin);
int F, P, C, M;
int i, j, k, sum, a, b;
int cows[101];
scanf("%d %d %d %d", &F, &P, &C, &M);
for (i = 0; i < F; i++)
{
for (j = 0; j < F; j++)
mat[i][j] = inf;
mat[i][i] = 0;
v[i] = false;
dist[i] = inf;
}
for (i = 0; i < P; i++)
{
scanf("%d %d %d", &a, &b, &k);
a--;
b--;
if (k < mat[a][b])
{
mat[a][b] = k;
mat[b][a] = k;
}
}
spfa(0,F);
sum = 0;
for(i = 0; i < C; i++)
{
scanf("%d",cows + i);
if(dist[cows[i]-1] <= M )
{
sum++;
cows[i] = 1;
}
else
cows[i] = 0;
}
printf("%d/n",sum);
for(i = 0; i < C; i++)
{
if(cows[i])
printf("%d/n",i+1);
}
return 0;
}
// Name : POJ_2394.cpp
// Author : tiger
// Version :
// Copyright : Your copyright notice
// Description : 最短路径---SPFA算法
//============================================================================
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 501;
const int inf = 100000000;
int mat[MAXN][MAXN];
bool v[MAXN];
int dist[MAXN];
void spfa(int s,int n)
{
queue<int> q;
q.push(s);
v[s] = true;
dist[s] = 0;
int t,i;
while (!q.empty())
{
t = q.front();
q.pop();
v[t] = false;
for (i = 0; i < n; i++)
{
if (dist[t] + mat[t][i] < dist[i])
{
dist[i] = dist[t] + mat[t][i];
if (!v[i])
{
v[i] = true;
q.push(i);
}
}
}
}
}
int main()
{
freopen("in", "r", stdin);
int F, P, C, M;
int i, j, k, sum, a, b;
int cows[101];
scanf("%d %d %d %d", &F, &P, &C, &M);
for (i = 0; i < F; i++)
{
for (j = 0; j < F; j++)
mat[i][j] = inf;
mat[i][i] = 0;
v[i] = false;
dist[i] = inf;
}
for (i = 0; i < P; i++)
{
scanf("%d %d %d", &a, &b, &k);
a--;
b--;
if (k < mat[a][b])
{
mat[a][b] = k;
mat[b][a] = k;
}
}
spfa(0,F);
sum = 0;
for(i = 0; i < C; i++)
{
scanf("%d",cows + i);
if(dist[cows[i]-1] <= M )
{
sum++;
cows[i] = 1;
}
else
cows[i] = 0;
}
printf("%d/n",sum);
for(i = 0; i < C; i++)
{
if(cows[i])
printf("%d/n",i+1);
}
return 0;
}
相关文章推荐
- POJ 1125 Stockbroker Grapevine (FloydWarshall 所有点对最短路径)
- poj 1724 ROADS条件限制下的最短路径
- 最短路径·三:SPFA算法 HihoCoder - 1093 (spfa无向图)
- poj 3268一道最短路径题
- POJ 3278 爬格子 (bfs求最短路径)
- POJ 2387 Til the Cows Come Home (图论,最短路径)
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
- 最短路径-Dijkstra(poj 1135)
- 最短路径----SPFA算法
- POJ 2251 Dungeon Master 三维最短路径
- POJ 1062 枚举单源最短路径(dijkstra算法)
- 最短路径:我的理解--SPFA算法
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)
- hihoCoder - 1093 - 最短路径·三:SPFA算法
- POJ 3464 最短路径与次短路径的条数
- The Doors(poj1556线段相交+最短路径)
- 蓝桥杯 道路和航路(SPFA算法求最短路径)
- POJ 1062 昂贵的聘礼(最短路径.Dijkstra)
- POJ_3083——贴左右墙DFS,最短路径BFS
- POJ 最短路径