hdu 3665(最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3665
思路:对海相邻的点进行标记即可,然后就是裸的最短路了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define inf 1<<30 8 bool mark[22]; 9 bool visited[22]; 10 int dist[22]; 11 int map[22][22]; 12 int n; 13 14 void spfa(){ 15 memset(mark,false,sizeof(mark)); 16 for(int i=0;i<n;i++)dist[i]=inf; 17 dist[0]=0;mark[0]=true; 18 queue<int>Q; 19 Q.push(0); 20 while(!Q.empty()){ 21 int u=Q.front(); 22 Q.pop(); 23 mark[u]=false; 24 for(int i=0;i<n;i++)if(u!=i){ 25 if(map[u][i]<inf&&dist[u]+map[u][i]<dist[i]){ 26 dist[i]=dist[u]+map[u][i]; 27 if(!mark[i]){ mark[i]=true;Q.push(i); } 28 } 29 } 30 } 31 } 32 33 34 int main(){ 35 // freopen("1.txt","r",stdin); 36 int m,p,s,l,ans; 37 while(~scanf("%d",&n)){ 38 for(int i=0;i<n;i++){ 39 for(int j=0;j<n;j++){ 40 i==j?map[i][j]=0:map[i][j]=inf; 41 } 42 } 43 memset(visited,false,sizeof(visited)); 44 for(int i=0;i<=n-1;i++){ 45 scanf("%d%d",&m,&p); 46 if(p)visited[i]=true; 47 while(m--){ 48 scanf("%d%d",&s,&l); 49 map[s][i]=map[i][s]=min(map[i][s],l); 50 } 51 } 52 spfa();ans=inf; 53 for(int i=0;i<n;i++)if(visited[i])ans=min(ans,dist[i]); 54 printf("%d\n",ans); 55 } 56 return 0; 57 }