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(0N10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi(0MiN - 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 题意: 给出一个无向图,多出的仅仅是某个结点是否临海,让我们判断从起点0开始,到最近的临海点距离。 思路: 完全可以在输入的时候记录所有临海的点,dij一遍后,在这些点中找出最小距离值即可。 AC :/*============================================================================= # # Author: liangshu - cbam # # QQ : 756029571 # # School : 哈尔滨理工大学 # # Last modified: 2015-09-06 14:39 # # Filename: hdu1015.cpp # # Description: # The people who are crazy enough to think they can change the world, are the ones who do ! =============================================================================*/ # #include<iostream> #include<sstream> #include<algorithm> #include<cstdio> #include<string.h> #include<cctype> #include<string> #include<cmath> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; const int maxn=30003; const int INF=0x1f1f1f1f; int n,m; //标记起始位置 string s; string e; set<int >xx; struct Edge { int from,to,dist; Edge(int u,int v,int d):from(u),to(v),dist(d) {} }; vector<Edge>edges;//存储边的结点信息 vector<int>G[maxn];//邻接表 bool done[maxn]; //标记是否访问过 int d[maxn]; //距离数组 struct heapnode //用于优先队列自定义 { int d,u; bool operator <(const heapnode rhs) const { return d > rhs.d; } heapnode(int dd,int uu):d(dd),u(uu) {} }; void init()//初始化必不可少 { for(int i=0; i<n; i++) G[i].clear(); edges.clear(); } void addedge(int from,int to,int dist) { edges.push_back(Edge(from,to,dist)); int m = edges.size(); G[from].push_back(m-1); } void dij( int s) { priority_queue<heapnode>Q; for(int i=0; i<=n; i++)//初始化距离数组 d[i]=INF; d[s]=0; memset(done ,0,sizeof(done)); Q.push( heapnode(0,s) ); while(!Q.empty()) { heapnode x = Q.top(); Q.pop(); int u=x.u; if(done[u])continue; done[u] = true; for(int i=0; i<G[u].size(); i++) { Edge& e=edges[G[u][i]];//取出来一条邻接边 //cout<<"u = "<<u<<" e.u = "<<e.to<<endl; if(d[e.to]>d[u]+e.dist) { d[e.to] = d[u] + e.dist; Q.push((heapnode(d[e.to],e.to))); } } } } int main() { while(scanf("%d",&n)!=EOF) { int t=1; int ans=0; init(); xx.clear();int a, b; for(int i=0; i<n ; i++) { scanf("%d%d",&a,&b); if(b == 1){ xx.insert(i); } if(a == 0)continue; for(int z = 0; z< a; z++){ int x, y; scanf("%d%d",&x, &y); addedge(i, x,y); addedge(x,i ,y); } } dij(0); int M = 0x1f1f1f1f; for(int i = 0; i < n ;i++){ if(xx.count(i)) { M = min(M, d[i]); } } cout<<M<<endl; s.clear(); e.clear(); } return 0; } /* 5 1 0 1 1 2 0 2 3 3 5 1 1 4 100 0 1 0 1 */