//给一个图,有些点近海,问从0点到近海点的最短路
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 100 ;
const int inf = 0x3f3f3f3f ;
int map[maxn][maxn] ;
int dis[maxn] ;
int vis[maxn] ;
int n , m ,pi;
int p[maxn] ;
void dijkstra()
{
memset(vis , 0 , sizeof(vis)) ;
for(int i = 1;i <= n;i++)
dis[i] = inf ;
dis[0] = 0 ;
for(int i = 1;i <=n;i++)
{
int mi = inf ;
int pos ;
for(int j = 0;j <= n;j++)
if(!vis[j] && dis[j] < mi)
mi = dis[pos = j] ;
vis[pos] = 1;
for(int j = 1;j <= n;j++)
dis[j] = min(dis[j] , dis[pos] + map[pos][j]) ;
}
}
int main()
{
while(~scanf("%d" ,&n))
{
for(int i = 0;i <= n;i++)
for(int j = 0;j <= n;j++)
map[i][j] = inf ;
for(int i = 0;i < n;i++)
{
scanf("%d%d" ,&m ,&pi) ;
p[i] = pi ;
while(m--)
{
int v , w;
scanf("%d%d" ,&v , &w);
map[i][v] = map[v][i] = w ;
}
}
dijkstra() ;
int ans = inf ;
for(int i = 1;i <= n;i++)
if(p[i])
ans = min(ans , dis[i]) ;
printf("%d\n" , ans) ;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-10 00:29:17