HDU 3665 Seaside
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                HDU 3665 Seaside
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                題目鏈接:
http://acm.split.hdu.edu.cn/showproblem.php?pid=3665
Problem Description XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.?
Input There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers SMi and LMi, which means that the distance between the i-th town and the SMi town is LMi.?
Output Each case takes one line, print the shortest length that XiaoY reach seaside.?
Sample Input 5 1 0 1 1 2 0 2 3 3 1 1 1 4 100 0 1 0 1?
Sample Output 2 Hint: 題意: 一個含有N個小城鎮的大城鎮,以0到N-1的順序,先給出兩個數a,b;表示i到其他城鎮有a條道路,b表示該地方是否有海,0表示沒有海,1表示有海。 然后在給出a1,x;a1表示i到a1之間有道路,距離是x。求從0到有海的城市的最小的距離。 題解: 簡單的最短路問題。 代碼: #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 10+10; #define met(a,b) memset(a,b,sizeof(a)) #define inf 0x3f3f3f3f int map[maxn][maxn]; int visited[maxn],dis[maxn]; int num[maxn]; int n,m; void dijkstra(int x) {int min=0,p=0;for(int i=0;i<n;i++){dis[i]=map[x][i];visited[i]=0;}visited[x]=1;for(int i=0;i<n;i++){min=inf;for(int j=0;j<n;j++){if(!visited[j]&&dis[j]<min){min=dis[j];p=j;}}visited[p]=1;for(int j=0;j<n;j++){if(!visited[j]&&dis[p]+map[p][j]<dis[j])dis[j]=dis[p]+map[p][j];}} } int main() {while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)for(int j=0;j<n;j++)map[i][j]=inf;met(num,0);for(int i=0;i<n;i++){scanf("%d%d",&m,&num[i]);for(int j=0;j<m;j++){int x,y;scanf("%d%d",&x,&y);map[i][x]=map[x][i]=y;}}dijkstra(0);int ans=inf;for(int i=0;i<n;i++){if(num[i]==1)ans=min(ans,dis[i]);}printf("%d\n",ans);} }
?
轉載于:https://www.cnblogs.com/TAT1122/p/5853273.html
總結
以上是生活随笔為你收集整理的HDU 3665 Seaside的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: js创建节点,小试牛刀
- 下一篇: Spring MVC 学习笔记一 Hel
