hdu3790

http://acm.hdu.edu.cn/showproblem.php?pid=3790

View Code
   
     
#include < iostream >
#define M 1001
#define Max 0x7fffffff
using namespace std;

int n,m;
int a,b,d,p;
int s,t;
int map[M][M];
int cost[M][M];
int de[M];
int co[M];
int visit[M];
int len;
int _min;

void Init()
{
int i,j;
for (i = 0 ;i <= n;i ++ )
{
for (j = 0 ;j <= n;j ++ )
{
map[i][j]
= Max;
cost[i][j]
= Max;
}
}
}

void DJ()
{
int i,j,p,k;

memset(visit ,
0 , sizeof (visit));
for (i = 1 ;i <= n;i ++ )
de[i]
= co[i] = Max;

de[s]
= 0 ;
co[s]
= 0 ;

for (i = 1 ;i <= n;i ++ )
{
_min
= Max; k = 0 ;

for (j = 1 ;j <= n;j ++ )
{
if ( ! visit[j] && de[j] < _min)
{
_min
= de[j];
k
= j;
}
}
if (_min == Max) break ;
visit[k]
= 1 ;
for ( p = 1 ; p <= n; p ++ )
{
if ( ! visit[p] && map[k][p] != Max)
{
if (de[p] > de[k] + map[k][p])
{
de[p]
= de[k] + map[k][p];
co[p]
= co[k] + cost[k][p];
}
else if ( de[p] == de[k] + map[k][p] && co[p] > co[k] + cost[k][p] )
{
co[p]
= co[k] + cost[k][p];
}
}
}
}
}


int main()
{
while ( cin >> n >> m, n + m )
{
Init();
int i;
for (i = 0 ;i < m;i ++ )
{
cin
>> a >> b >> d >> p;

if ( map[a][b] > d)
{
map[a][b]
= map[b][a] = d;
cost[a][b]
= cost[b][a] = p;
}
}
cin
>> s >> t;
DJ();
cout
<< de[t] << " " << co[t] << endl;
}
return 0 ;
}

wrong answer的代码:帮我看看错哪儿了?哎,有什么特殊例子啊

View Code
   
     
#include < iostream >
#define M 1001
#define Max 0x7fffffff
using namespace std;

int n,m;
int a,b,d,p;
int s,t;
int map[M][M];
int cost[M][M];
int visit[M];
int _min;

void Init()
{
int i,j;
for (i = 0 ;i <= n;i ++ )
{
for (j = 0 ;j <= n;j ++ )
{
map[i][j]
= Max;
cost[i][j]
= Max;
}
}
}

void DJ()
{
int i,j,p,k;

for (i = 1 ;i <= n;i ++ )
{
_min
= Max; k = 0 ;

for (j = 1 ;j <= n;j ++ )
{
if ( ! visit[j] && map[ 0 ][j] < _min)
{
_min
= map[ 0 ][j];
k
= j;
}
}
if (_min == Max) break ;

visit[k]
= 1 ;
for ( p = 1 ; p <= n; p ++ )
{
if ( ! visit[p] && map[k][p] != Max)
{
if (map[ 0 ][p] > map[ 0 ][k] + map[k][p])
{
map[
0 ][p] = map[ 0 ][k] + map[k][p];
cost[
0 ][p] = cost[ 0 ][k] + cost[k][p];
}
else if ( map[ 0 ][p] = map[ 0 ][k] + map[k][p] && cost[ 0 ][p] > cost[ 0 ][k] + cost[k][p] )
{
cost[
0 ][p] = cost[ 0 ][k] + cost[k][p];
}
}
}
}
}


int main()
{
while ( cin >> n >> m, n + m )
{
Init();
int i;
for (i = 0 ;i < m;i ++ )
{
cin
>> a >> b >> d >> p;

if ( map[a][b] > d)
{
map[a][b]
= map[b][a] = d;
cost[a][b]
= cost[b][a] = p;
}
}
cin
>> s >> t;
map[
0 ][s] = 0 ;
cost[
0 ][s] = 0 ;
memset(visit ,
0 , sizeof (visit));
DJ();
cout
<< map[ 0 ][t] << " " << cost[ 0 ][t] << endl;
}
return 0 ;
}

你可能感兴趣的:(HDU)