poj 2449 Remmarguts' Date 第k短路 (最短路变形)

Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 33606   Accepted: 9116

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 2
1 2 5
2 1 4
1 2 2

Sample Output

14

这题就是一个第k短路的模板题,
主要是运用Astar算法,启发式搜索,
就是相当于一个剪枝,优化的非常明显,

struct A
{
     int v,g,f;
     bool operator < (const A a)const {
               if (a.f==f ) return a.g<g;
               return a.f<f;
      }
};

v表示所在点,g表示到达v点所走的距离,

  f表示 走到终点 的距离,

  得到f 就需要反跑一遍最短路,d[maxn]就是用来存储这个距离的

  所以 f=g+d[s] 

  这题其实我一开始爆内存了好多发  ,找了一天发现是我重载写的有问题 

  但是我不知道问题到底在那里 

  有 大佬指点下吗  

struct A {
         int f, g, v;
         friend bool operator <(A a, A b) {
         if (a.f == b.f ) return a.g < b.g;
         return a.f < b.f;
        }
};

希望有大佬能告诉我 这样写重载为什么会导致爆内存! 

 表示我特别迷啊 ,卡了一天结果是这里有问题 ,

 更加可悲的事,我还不知道原因是什么。

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 #include <queue>
  6 
  7 using namespace std;
  8 const int maxn =1e3+10;
  9 const int maxm = 5e5+10;
 10 const int inf = 1e9+7;
 11 struct node
 12 {
 13     int v,w,next;
 14 }edge[maxm],redge[maxm];
 15 int vis[maxn],d[maxn],head[maxn],rhead[maxn];
 16 int e,s,t,n,m,k;
 17 struct A
 18 {
 19     int v,g,f;
 20     bool operator < (const A a)const {
 21         if (a.f==f ) return a.g<g;
 22         return a.f<f;
 23     }
 24 };
 25 void init()
 26 {
 27     e=0;
 28     memset(head,-1,sizeof(head));
 29     memset(rhead,-1,sizeof(rhead));
 30 }
 31 void add(int x,int y,int z)
 32 {
 33     edge[e].v=y;
 34     edge[e].w=z;
 35     edge[e].next=head[x];
 36     head[x]=e;
 37     redge[e].v=x;
 38     redge[e].w=z;
 39     redge[e].next=rhead[y];
 40     rhead[y]=e;
 41     e++;
 42 }
 43 void spfa(int s)
 44 {
 45     for (int i=1 ;i<=n ;i++) d[i]=inf;
 46     memset(vis,0,sizeof(vis));
 47     d[s]=0;
 48     vis[s]=1;
 49     queue<int>q;
 50     q.push(s);
 51     while(!q.empty()){
 52         int u=q.front();
 53         q.pop();
 54         vis[u]=0;
 55         for (int i=rhead[u] ;i!=-1 ;i=redge[i].next) {
 56             int v=redge[i].v;
 57             int w=redge[i].w;
 58             if (d[v]>d[u]+w){
 59                 d[v]=d[u]+w;
 60                 if (!vis[v]) {
 61                     q.push(v);
 62                     vis[v]=1;
 63                 }
 64             }
 65         }
 66     }
 67 }
 68 int Astar(int s,int des)
 69 {
 70     int cnt=0;
 71     if (s==des) k++;
 72     if (d[s]==inf) return -1;
 73     priority_queue<A>q;
 74     A t,tt;
 75     t.v=s,t.g=0,t.f=t.g+d[s];
 76     q.push(t);
 77     while(!q.empty()){
 78         tt=q.top();
 79         q.pop();
 80         if (tt.v==des) {
 81             cnt++;
 82             if (cnt==k) return tt.g;
 83         }
 84         for (int i=head[tt.v] ;i!=-1 ; i=edge[i].next ){
 85             t.v=edge[i].v;
 86             t.g=edge[i].w+tt.g;
 87             t.f=t.g+d[t.v];
 88             q.push(t);
 89         }
 90     }
 91     return -1;
 92 }
 93 int main() {
 94     while(scanf("%d%d",&n,&m)!=EOF ){
 95         init();
 96         for (int i=1 ;i<=m ;i++) {
 97             int x,y,c;
 98             scanf("%d%d%d",&x,&y,&c);
 99             add(x,y,c);
100         }
101         scanf("%d%d%d",&s,&t,&k);
102         spfa(t);
103         printf("%d\n",Astar(s,t));
104     }
105     return 0;
106 }

猜你喜欢

转载自www.cnblogs.com/qldabiaoge/p/8934410.html