您的位置:首页 > 其它

HDOJ 3790 最短路径问题 SPFA

2012-08-09 14:50 393 查看
因为没有对边判重,白白WA了好几次,注意输入时必须要对边判重

AC 187MS 8100K

#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
using namespace std;

const int maxn = 1001;
const int INF = 0x3F3F3F3F;

int a, b, d, p, nNum, mNum;
int dist[maxn], px[maxn], QM[maxn];
int cost[maxn][maxn], g[maxn][maxn];

void spfa(int sx, int tx)
{
int   xx;
queue <int> q;

q.push(sx);
QM[sx]=1, dist[sx]=0, px[sx]=0;

while (!q.empty())
{
xx = q.front();
q.pop();
QM[xx] = 0; /* Took Away */

for (int i=1; i<=nNum; ++i)
{
if (dist[i] > dist[xx]+g[xx][i])
{
dist[i] = dist[xx] + g[xx][i];
px[i] = px[xx] + cost[xx][i];

if (QM[i] == 0)
{
QM[i] = 1;
q.push(i);
}
}/* End of if */
else if (dist[i]==dist[xx]+g[xx][i]
&& px[i]>px[xx]+cost[xx][i])
{
px[i] = px[xx] + cost[xx][i];
}
}/* End of For */
}/* End of While */

printf("%d %d\n", dist[tx], px[tx]);
}/* spfa */

int main()
{
while (~scanf("%d %d", &nNum, &mNum), nNum+mNum!=0)
{
for (int i=1; i<=nNum; ++i)
{
QM[i] = 0;
px[i] = dist[i] = INF;

for (int j=1; j<=nNum; ++j)
g[i][j] = cost[i][j] = INF;
}/* End of For */

for (int i=1; i<=mNum; ++i)
{
scanf("%d %d %d %d", &a, &b, &d, &p);

if (d < g[a][b])  /* 判重 */
{
g[a][b] = g[b][a] = d;
cost[a][b] = cost[b][a] = p;
}
}/* End of For */

int sx, tx;

scanf("%d %d", &sx, &tx);
spfa(sx, tx);
}/* End of While */

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: