zoukankan      html  css  js  c++  java
  • poj 2449 第K最短路-A*启发式搜索

    http://poj.org/problem?id=2449

    【题意】:给出一张有重复边的有向图,求其第k短路

    【算法参考】:http://blog.sina.com.cn/s/blog_60707c0f010109q5.html

      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<queue>
      5 #define maxx 9999999
      6 using namespace std;
      7 
      8 
      9 struct edge{int v,l;};
     10 struct node{int g,v;};
     11 priority_queue<node > q;
     12 vector<edge> graph1[1002];
     13 vector<edge> graph2[1002];
     14 int d[1002],n,vis[1002];
     15 
     16 bool operator < (const node& a,const node& b)//  在优先队列中 用于比较的标准是s到当前点的实际距离+该点到t的最短路
     17 {
     18     return (a.g+d[a.v] > b.g+d[b.v]);
     19 }
     20 
     21 int dij(int s,int t)
     22 {
     23     memset(vis,0,sizeof(vis));
     24     for(int i=0;i<=n;i++)
     25         d[i]=maxx;
     26     d[t]=0;
     27     for(int i=0;i<n;i++)
     28     {
     29         int minn=maxx,k=1;
     30         for(int j=1;j<=n;j++)
     31             if(vis[j]==0&&minn>d[j])
     32              {
     33                  minn=d[j];
     34                  k=j;
     35              }
     36              vis[k]=1;
     37              for(int j=0;j<graph2[k].size();j++)
     38              {
     39                  int v;
     40                  v=graph2[k][j].v;
     41                  if(d[v]>graph2[k][j].l+d[k])
     42                     d[v]=graph2[k][j].l+d[k];
     43 
     44              }
     45     }
     46     if(d[s]==maxx)
     47        return 0;
     48     return 1;
     49 
     50 }
     51 
     52 int solve(int s,int t,int k)
     53 {
     54     node temp,temp2;
     55     temp.g=0;
     56     temp.v=s;
     57     q.push(temp);
     58     memset(vis,0,sizeof(vis));
     59     while(!q.empty())
     60     {
     61         temp=q.top(); q.pop();
     62         int vv;
     63         vv=temp.v;
     64         vis[vv]++;
     65         if(vis[vv]==k)
     66             return temp.g+d[vv];
     67 
     68         for(int i=0;i<graph1[vv].size();i++)
     69         {
     70             temp2.g=temp.g+graph1[vv][i].l;
     71             temp2.v=graph1[vv][i].v;
     72             q.push(temp2);
     73         }
     74 
     75     }
     76     return -1;
     77 }
     78 
     79 
     80 
     81 int main()
     82 {
     83     int i,j,s,t,k,m,a,b,c;
     84     while(~scanf("%d%d",&n,&m))
     85     {
     86         for(int i=0;i<=n;i++)
     87         {
     88             graph1[i].clear();
     89             graph2[i].clear();
     90         }
     91         while(m--)
     92         {
     93             scanf("%d%d%d",&a,&b,&c);
     94             edge temp;
     95             temp.v=b;temp.l=c;
     96             graph1[a].push_back(temp);
     97             temp.v=a;
     98             graph2[b].push_back(temp);//添加反向边 用于求每一点到t的最短路
     99         }
    100         scanf("%d%d%d",&s,&t,&k);
    101         if(s==t)   /////?????????????????特别注意 :当s==t时 最短路也不是0,一定要走!!!!!!!
    102             k++;
    103         if(dij(s,t)==0)
    104             printf("-1");
    105         else
    106             printf("%d
    ",solve(s,t,k));
    107 
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    函数式编程与命令式编程的学习难度比较
    Swfit4.0中JSON与模型原生互转(JSONEncoder/JSONDecoder的使用)
    元类型与类型的区别
    Swift
    swift类型操作规范
    PHP实现执行定时任务的几种思路详解
    基于ThinkPHP与阿里大于的PHP短信验证功能
    laravel中将session由文件保存改为数据库保存
    laravel5.*安装使用Redis以及解决Class 'PredisClient' not found和Fatal error: Non-static method Redis::set() cannot be called statically错误
    Python基础知识汇总
  • 原文地址:https://www.cnblogs.com/assult/p/3853006.html
Copyright © 2011-2022 走看看