[2016-04-02][POJ][2387][Til the Cows Come Home]

  • 时间:2016-04-02 10:34:36 星期六

  • 题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home]

  • 题目大意:给定n个节点和t条路,求n到1的最短路长度

  • 分析:跑一次最短路即可

  • 遇到的问题:

    • 据说是多重边,如果是用邻接矩阵的就要更新最小值,
    • 此题是先输入t,再输入n,输入的时候读错,无限WA…
  1. #include <queue>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int maxn = 1000 + 10;
  6. const int maxe = 2000 + 10;
  7. struct mNode{
  8. int u,c;
  9. mNode(int _u = 0,int _c = 0):u(_u),c(_c){}
  10. bool operator < (const mNode & a)const{
  11. return c > a.c;
  12. }
  13. };
  14. struct Edge{
  15. int v,c;
  16. Edge(int _v = 0,int _c = 0):v(_v),c(_c){}
  17. };
  18. vector<Edge> e[maxe];
  19. bool vis[maxn];
  20. int d[maxn];
  21. void Dijkstra(int s){
  22. memset(vis,0,sizeof(vis));
  23. memset(d,0x3f,sizeof(d));
  24. priority_queue<mNode> q;
  25. d[s] = 0;
  26. q.push(mNode(s,0));
  27. mNode tmp;
  28. while(!q.empty()){
  29. tmp = q.top();
  30. q.pop();
  31. int u = tmp.u;
  32. if(vis[u]) continue;
  33. vis[u] = 1;
  34. for(int i = 0;i < e[u].size();++i){
  35. Edge & me = e[u][i];
  36. if(!vis[me.v] && d[me.v] > d[u] + me.c){
  37. d[me.v] = d[u] + me.c;
  38. q.push(mNode(me.v,d[me.v]));
  39. }
  40. }
  41. }
  42. }
  43. inline void addedge(int u,int v,int c){
  44. e[u].push_back(Edge(v,c));
  45. }
  46. int main(){
  47. int n,t,a,b,c;
  48. scanf("%d%d",&t,&n);
  49. for(int i = 0;i < t; ++i){
  50. scanf("%d%d%d",&a,&b,&c);
  51. addedge(a,b,c);
  52. addedge(b,a,c);
  53. }
  54. Dijkstra(n);
  55. printf("%d\n",d[1]);
  56. return 0;
  57. }

来自为知笔记(Wiz)

时间: 2024-10-20 19:17:09

[2016-04-02][POJ][2387][Til the Cows Come Home]的相关文章

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

POJ 2387 Til the Cows Come Home (Dijkstra)

题目链接:POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possib

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted: 11174 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home

题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get ba

poj 2387 Til the Cows Come Home(最短路)

题目链接:http://poj.org/problem?id=2387 题目大意:求点1到点n的最短距离 1.Dijkstras算法 #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 1010 #define INF 0x3f3f3f int map[maxn][maxn]; int vis[maxn]; //标记最短路的点 int dis