• poj 2449(Remmarguts' Date)


    View Code
      1 #include<stdio.h>
      2 #include<iostream>
      3 #include<queue>
      4 #include<string.h>
      5 using namespace std;
      6 #define inf 0x7ffffff
      7 #define N 100010
      8 #define M 1010
      9 struct edge{
     10     int w,to,next;
     11 }e1[N],e2[N];
     12 struct node{
     13     int w,u;
     14     bool operator <(const node &x)const
     15     {
     16         return w>x.w;
     17     }
     18 };
     19 struct node1{
     20     int f,g,u;
     21     bool operator <(const node1 &x)const
     22     {
     23         return f>x.f;
     24     }
     25 };
     26 int cnt;
     27 int pre1[M],pre2[M],dist[M];
     28 void add(int a,int b,int w)
     29 {
     30     e1[cnt].next=pre1[a];
     31     e1[cnt].w=w;
     32     e1[cnt].to=b;
     33     pre1[a]=cnt;
     34     e2[cnt].next=pre2[b];
     35     e2[cnt].w=w;
     36     e2[cnt].to=a;
     37     pre2[b]=cnt++;
     38 }
     39 void dijstra(int s,int n,int pre[],edge e[])
     40 {
     41     priority_queue<node> q;
     42     for(int i=0;i<=n;i++)
     43     dist[i]=inf;
     44     dist[s]=0;
     45     node in;
     46     in.w=0;
     47     in.u=s;
     48     q.push(in);
     49     while(!q.empty())
     50     {
     51         node out=q.top();
     52         q.pop();
     53         int u=out.u;
     54         int w=out.w;
     55         for(int edg=pre[u];edg!=0;edg=e[edg].next)
     56         {
     57             int v=e[edg].to;
     58             if(dist[v]>dist[u]+e[edg].w)
     59             {
     60                 dist[v]=dist[u]+e[edg].w;
     61                 in.w=dist[v];
     62                 in.u=v;
     63                 q.push(in);
     64             }
     65         }
     66     }
     67 }
     68 int Astar(int s,int t,int k,int pre[],edge e[])
     69 {
     70     priority_queue<node1> q;
     71     int cnt1=0;
     72     node1 in;
     73     in.u=s;
     74     in.g=0;
     75     in.f=dist[s];
     76     q.push(in);
     77     while(!q.empty())
     78     {
     79         node1 out=q.top();
     80         q.pop();
     81         int g=out.g;
     82         int u=out.u;
     83         if(u==t)cnt1++;
     84         if(cnt1==k)return out.f;
     85         for(int edg=pre[u];edg!=0;edg=e[edg].next)
     86         {
     87             int v=e[edg].to;
     88             in.u=v;
     89             in.g=g+e[edg].w;
     90             in.f=in.g+dist[v];
     91             q.push(in);
     92         }
     93     }
     94     return -1;
     95 }
     96 int main()
     97 {
     98     int m,n,k;
     99     while(scanf("%d%d",&n,&m)!=EOF)
    100     {
    101         memset(pre1,0,sizeof(pre1));
    102         memset(pre2,0,sizeof(pre2));
    103         cnt=1;
    104         int a,b,w;
    105         while(m--)
    106         {
    107             scanf("%d%d%d",&a,&b,&w);
    108             add(a,b,w);
    109         }
    110         int s,t,k;
    111         scanf("%d%d%d",&s,&t,&k);
    112         if(s==t)k++;
    113         dijstra(t,n,pre2,e2);
    114         printf("%d\n",Astar(s,t,k,pre1,e1));
    115     }
    116     return 0;
    117 }
  • 相关阅读:
    slf4j的简单用法以及与log4j的区别
    [转]Git 代码撤销、回滚到任意版本(当误提代码到本地或master分支时)
    【转】IDEA 中配置文件properties文件中文乱码解决
    Python+Selenium练习篇之3-浏览器滚动条操作
    selenium操作下拉滚动条的几种方法
    python利用unittest进行测试用例执行的几种方式
    安装和使用 Python
    PM2实用入门指南
    linux清除缓存
    【centos6.6环境搭建】Github unable to access SSL connect error出错处理
  • 原文地址:https://www.cnblogs.com/huangriq/p/2447068.html
Copyright © 2020-2023  润新知