poj2449 k Short Circuit problem

Source: Internet
Author: User

The problem is to let you find the S-point to the T-point K short, using a * search, the search uses two indicator functions H g, h represents the shortest path from the source point to the current point, and the G-point indicates the shortest path from the current point to the sink point, and the h at the time of the search for the K-vertex is the length of the K-short, the code is as follows:

#include <cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>using namespacestd;Const intMAXN = ++Ten;intN, M;intS, T, K;structEdge {intV, c;}; Vector<edge> G[MAXN];//Forward EdgeVector<edge> P[MAXN];//Reverse EdgeintG[MAXN];//solve the value of the H functionstructdij{intu, c; BOOL operator< (ConstDIJ&AMP;R)Const    {        returnC>r.c; }};BOOLVIS[MAXN];voidDijkstraints) {memset (g,0x3f,sizeof(g)); G[s]=0; memset (Vis,0,sizeof(VIS)); Priority_queue<Dij>que; Que.push ((DIJ) {s,0});  while(!Que.empty ()) {DIJ TP=que.top (); Que.pop (); intu=tp.u; if(Vis[u])Continue; Vis[u]=1;  for(intI=0; I<p[u].size (); i++)        {            intv = p[u][i].v, cost =p[u][i].c; if(G[v] > g[u]+Cost ) {G[v]= g[u]+Cost ;            Que.push ((DIJ) {V, g[v]}); }        }    }}structastar{inth, G, u; BOOL operator< (Constastar& R)Const    {        returnH+g > r.h+R.G; }};intTIMES[MAXN];intAstar () {memset (Times,0,sizeof(times)); Priority_queue<Astar>que; Que.push ((Astar) {0, G[s], S}); if(S = = T) k++;  while(!Que.empty ()) {Astar TP=que.top (); Que.pop (); intU =tp.u; Times[u]++; if(Times[u]==k && u==t)returnTp.h; Else if(Times[u] > K)Continue;  for(intI=0; I<g[u].size (); i++)        {            intv = g[u][i].v, c=g[u][i].c; Que.push ((Astar) {tp.h+c, G[v], v}); }    }    return-1;}intMain () {scanf ("%d%d", &n, &M);  for(intI=0; i<m; i++)    {        intu, V, t; scanf ("%d%d%d", &u, &v, &t);        G[u].push_back (Edge) {V, t});    P[v].push_back (Edge) {u, t}); } scanf ("%d%d%d", &s, &t, &j);    Dijkstra (T); intres =Astar (); printf ("%d\n", RES); return 0;}

poj2449 k Short Circuit problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.