第二次
/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2011 panyanyany All rights reserved.
URL : http://acm.hdu.edu.cn/showproblem.php?pid=1874
Name : 1874 畅通工程续
Date : Friday, August 19, 2011
Time Stage : half an hour
Result:
4453851 2011-08-19 21:39:38 Accepted 1874
31MS 4192K 1660 B
C++ pyy
Test Data:
Review:
那句相当重要的还是忘了,结果WA
//----------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define infinity 0x0f0f0f0f
#define minus_inf 0x80808080
#define MAXSIZE 1009
int city, road, start, end ;
int map[MAXSIZE][MAXSIZE], dp[MAXSIZE], used[MAXSIZE] ;
void dijkstra ()
{
int i, j ;
int iMinPath, minPath ;
memset (used, 0, sizeof (used)) ;
for (i = 0 ; i < city ; ++i)
dp[i] = map[start][i] ;
dp[start] = 0 ; // 此句相当重要,不加则WA
for (i = 0 ; i < city ; ++i)
{
minPath = infinity ;
for (j = 0 ; j < city ; ++j)
{
if (!used[j] && dp[j] < minPath)
{
iMinPath = j ;
minPath = dp[j] ;
}
}
used[iMinPath] = 1 ;
for (j = 0 ; j < city ; ++j)
{
if (!used[j] && dp[iMinPath] + map[iMinPath][j] < dp[j])
dp[j] = dp[iMinPath] + map[iMinPath][j] ;
}
}
}
int main ()
{
int i ;
int a, b, c ;
while (scanf ("%d%d", &city, &road) != EOF)
{
memset (map, infinity, sizeof (map)) ;
for (i = 0 ; i < road ; ++i)
{
scanf ("%d%d%d", &a, &b, &c) ;
map[a][b] = map[b][a] = min (map[a][b], c) ;
}
scanf ("%d%d", &start, &end) ;
dijkstra () ;
if (dp[end] == infinity)
printf ("-1\n") ;
else
printf ("%d\n", dp[end]) ;
}
return 0 ;
}
第一次
/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2011 panyanyany All rights reserved.
URL : http://acm.hdu.edu.cn/showproblem.php?pid=1874
Name : 1874 畅通工程续
Date : Saturday, May 14, 2011
Time Stage : 1 hours around
Result:
3953043 2011-05-14 10:57:25 Accepted 1874 15MS 460K 1462 B C++ pyy
Test Data:
Review:
//----------------------------------------------------------------------------*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int size = 210, infinite = 0x0f0f0f0f;
int city, path, from, to;
int dist[size], map[size][size], hash[size];
void dijkstra()
{
int i, j, k;
memset( hash, 0, sizeof(hash) );
for( i = 0; i < city; ++i )
dist[i] = map[i][to];
hash[to] = 1;
dist[to] = 0; // 这句不加,就一直WA
for( i = 0; i < city; ++i )
{
int minPath = infinite;
int iMinPath = 0;
for( j = 0; j < city; ++j )
if( !hash[j] && dist[j] < minPath )
{
iMinPath = j;
minPath = dist[j];
}
hash[iMinPath] = 1;
for( j = 0; j < city; ++j )
if( !hash[j] && dist[iMinPath] + map[iMinPath][j] < dist[j] )
dist[j] = dist[iMinPath] + map[iMinPath][j];
}
}
int main()
{
int i, j, k, cost;
while( cin >> city >> path )
{
memset( map, infinite, sizeof(map) );
for( i = 0; i < path; ++i )
{
cin >> j >> k >> cost;
map[j][k] = map[k][j] = min( map[j][k], cost );
}
cin >> from >> to;
dijkstra();
if( dist[from] == infinite )
dist[from] = -1;
cout << dist[from] << endl;
}
return 0;
}