(WA)POJ2449 第K短路

POJ2449 第K短路

  • 改了好长时间发现读入读反了qwq
  • A*,先在反向图上求出每个点到t的最短距离,作为估价函数即可
  • 疑问:能不能直接记录h+g
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cctype>
  4 #include <algorithm>
  5 #include <queue>
  6 #include <iostream>
  7 using namespace std;
  8 
  9 #define N 10010
 10 #define M 100010
 11 #define inf 0x3f3f3f3f
 12 #define res register int
 13 inline int read()
 14 {
 15     int x(0),f(1); char ch;
 16     while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
 17     while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
 18     return f*x;
 19 }
 20 
 21 int n,m,k,s,t;
 22 int head[N],ver[M<<1],nxt[M<<1],edge[M<<1];
 23 int head2[N],ver2[M<<1],nxt2[M<<1],edge2[M<<1];
 24 int tot,tot2;
 25 inline void add(int x,int y,int z)
 26 {
 27     ver[++tot]=y; nxt[tot]=head[x]; head[x]=tot ; edge[tot]=z;
 28     ver2[++tot2]=x; nxt2[tot2]=head2[y]; head2[y]=tot2; edge2[tot2]=z;
 29 }
 30 int f[N],vis[N],dis[N];
 31 struct node{
 32     int id,dis;
 33     bool operator <(const node &n2)const{
 34     return dis>n2.dis;}
 35 };
 36 struct node2{
 37     int id,h,g;
 38     bool operator <(const node2 &n2)const{
 39     return h+g>n2.h+n2.g;}
 40 };
 41 
 42 inline void get_f()
 43 {
 44     memset(f,0x3f,sizeof(f));
 45     priority_queue<int,vector<int>,greater<int> > q;
 46     q.push(t); f[t]=0; vis[t]=1;
 47     while(q.size())
 48     {
 49         int x=q.top(); q.pop(); vis[x]=0;
 50         for(res i=head2[x] ; ~i ; i=nxt2[i])
 51         {
 52             int y=ver2[i];  
 53             if(f[y]>f[x]+edge2[i])
 54             {
 55                 f[y]=f[x]+edge2[i];
 56                 if(!vis[y])
 57                     vis[y]=1,q.push(y);
 58             }
 59         }
 60     }
 61 //    if(f[s]==inf) {
 62 //        puts("-1"); exit(0);
 63 //    }
 64 }
 65 
 66 
 67 
 68 int cnt[N];
 69 inline int dij()
 70 {
 71     priority_queue<node2> q;
 72     memset(vis,0,sizeof(vis)); 
 73     q.push((node2){s,0,f[s]});    
 74     while(q.size())
 75     {
 76         node2 now=q.top(); q.pop(); int x=now.id;
 77         cnt[x]++;
 78         if(cnt[x]==k && x==t) return now.h+now.g;
 79         if(cnt[x]>k) continue;
 80         for(res i=head[x] ; ~i ; i=nxt[i])
 81         {
 82             node2 tmp; int y=ver[i];
 83             tmp.id=y;
 84             tmp.h=now.h+edge[i];
 85             tmp.g=f[y];
 86 //            tmp.dis=now.dis-f[x]+edge[i]+f[y];
 87             q.push(tmp);
 88         }
 89     }
 90     return -1;
 91 }
 92 
 93 int main()
 94 {
 95     n=read(); m=read(); 
 96     memset(head,-1,sizeof(head));
 97     memset(head2,-1,sizeof(head2));
 98     for(res i=1 ; i<=m ; i++)
 99     {
100         int x=read(),y=read(),z=read();
101         add(x,y,z);
102     }
103     s=read(); t=read(); k=read();
104     if(s==t) k++;
105     get_f();
106     printf("%d\n",dij());
107     return 0;
108 }
View Code

猜你喜欢

转载自www.cnblogs.com/wmq12138/p/10390116.html