哈理工练习赛 UVALive 5093 F - Seaside(最短路)
来源:互联网 发布:增加力量 知乎 编辑:程序博客网 时间:2024/06/06 01:14
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 toN - 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 integerN(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 areMi roads beginning with the i-th town. Pi indicates whether thei-th town is near to the sea, Pi = 0 means `No', Pi = 1 means `Yes'. In nextMi lines, each line contains two integersSMi and LMi, which means that the distance between thei-th town and the SMi town isLMi.
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; }/*51 01 12 02 33 51 14 1000 10 1 */
- 哈理工练习赛 UVALive 5093 F - Seaside(最短路)
- Seaside 3665 (最短路)
- String 长春理工(练习赛hdoj diy)最短路
- 哈理工练习赛 UVALive 5089 B - Assignments(水题)
- HDOJ 3665 Seaside(最短路--floyd)
- HDU 3665 Seaside (最短路--floyd)
- HDU 3665 Seaside 最短路
- hdu 3665 Seaside(最短路)
- hdu 3665 Seaside 最短路
- 杭电 3665 Seaside(最短路)(弗洛伊德过)
- Seaside最短路,找到最近靠海地)
- HDOJ 3665 Seaside(dijkstra最短路,水题~)
- hdu 3665 Seaside【裸最短路】
- HDU 3665 Seaside (Floyd 最短路)
- hdoj 3665 Seaside 【裸最短路】
- hdoj 3665 Seaside 【最短路&&SPFA】
- HDU Problem 3665 Seaside【最短路】
- 【ZSTU4215 2015年12月浙理工校赛 F】【Dijkstra最短路】Landlocked 到达海洋最少跨越国家数
- 选择排序
- C#DataGridView单元格Header背景颜色设置
- 关于include里加入根目录的问题
- jquery基础(7)动画特效
- NGUI与3d模型的<三明治>问题
- 哈理工练习赛 UVALive 5093 F - Seaside(最短路)
- fir.im Weekly - 每个程序员都应当拥有的技能树
- Python线程指南
- ZOJ 3785 What day is that day?
- 关于强制换行的问题
- 【Java】中文转换汉语拼音代码
- 精通Hibernate——报表查询、技巧以及优化
- [刷题]Single Number
- GIT设置SSH公钥