A * is a search, heuristic search, that is: each search with an evaluation function
This algorithm can be used to solve the K short-circuit problem, the commonly used valuation function is: The distance has been traversed + expected to the shortest distance
Usually solved with Dijkstra K short Circuit
BZOJ1598: Cow Running
K Short Circuit before seeking
Because A * algorithm we use each time to outward expansion is the least value function point, then we must be able to get, the first to reach N is the shortest way. (Dijkstra of greed, can be proven)
So, let's think about it, the second one to reach N is a short circuit!
From this view: K to arrive is a K short circuit
Therefore, a * algorithm can be used to solve the K short circuit problem
Attached code:
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib > #include <queue> #include <iostream> #include <map>using namespace std; #define N 10005#define LL Long longstruct node{int To,next,val;} E[n*10],e[n*10];int head[n],cnt,cnt1,n,m,k,vis[n],head1[n];int dis[n];void Add (int x,int y,int z) {e[cnt].to=y;e[cnt] . next=head[x];e[cnt].val=z;head[x]=cnt++;} void add1 (int x,int y,int z) {e[cnt1].to=y; E[CNT1].NEXT=HEAD1[X]; e[cnt1].val=z;head1[x]=cnt1++;} priority_queue<pair<int, int > >q;void Dijkstra () {memset (dis,0x3f,sizeof (dis));d Is[n]=0;q.push (make_ Pair (0,n)), while (!q.empty ()) {int x=q.top (). Second;q.pop (), if (Vis[x]) continue;vis[x]=1;for (int i=head1[x];i!=-1;i =e[i].next) {int to1=e[i].to;if (dis[to1]>dis[x]+e[i].val) {Dis[to1]=dis[x]+e[i].val;q.push (Make_pair (-dis[to1) , to1));}}} void Astar (int s) {Q.push (Make_pair (-dis[s],s)), while (!q.empty ()) {int d=q.top (). First;int x=q.top (). Second;q.pop (); if (x==n) {k--;p rinTF ("%d\n",-D); K) return;} for (int i=head[x];i!=-1;i=e[i].next) {int To1=e[i].to;q.push (Make_pair (d+dis[x]-e[i].val-dis[to1],to1));}}} int main () {memset (head1,-1,sizeof (HEAD1)); Memset (Head,-1,sizeof (head)); scanf ("%d%d%d", &n,&m,&k); for (int i=1;i<=m;i++) {int x,y,z;scanf ("%d%d%d", &x,&y,&z), add (y,x,z); Add1 (x, y, z);} Dijkstra (); Astar (1); while (k--) puts ("1"); return 0;}
1975: [Sdoi2010] Academy of Magical Pigs
This greedy greed is obviously, in order to be as much as possible, natural is to choose the front K short
So, again, we use the greedy method of Dijkstra to find the first k short Circuit of n
But the actual time complexity of this method is not very right, theoretically can be a lot of problems. This problem can be bzoj, but it's the valley.
An imperfect code is attached:
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib > #include <queue> #include <iostream> #include <map>using namespace std; #define N 5005#define LL Long Longstruct node{int to,next;double val;} E[200005],e[200005];int head[n],cnt,cnt1,n,m,vis[n],head1[n],ans;double dis[n],k;void Add (int x,int y,double z) {e[ cnt].to=y;e[cnt].next=head[x];e[cnt].val=z;head[x]=cnt++;} void add1 (int x,int y,double z) {e[cnt1].to=y; E[CNT1].NEXT=HEAD1[X]; e[cnt1].val=z;head1[x]=cnt1++;} priority_queue<pair<double, int > >q;void Dijkstra () {for (int i=0;i<n;i++) Dis[i]=1e9;dis[n]=0;q.push ( Make_pair (0,n)), while (!q.empty ()) {int x=q.top (). Second;q.pop (), if (Vis[x]) continue;vis[x]=1;for (int i=head1[x];i! =-1;i=e[i].next) {int to1=e[i].to;if (dis[to1]>dis[x]+e[i].val) {Dis[to1]=dis[x]+e[i].val;q.push (Make_pair (-dis [to1],to1));}}} void Astar (int s) {Q.push (Make_pair (-dis[s],s)), while (!q.empty ()) {double D=q.top (). First;int X=q.top (). Second;q.pop (); if (x==n) {if (k+d<0-1e-9) return; k+=d;ans++;} for (int i=head[x];i!=-1;i=e[i].next) {int To1=e[i].to;q.push (Make_pair (d+dis[x]-e[i].val-dis[to1],to1));}}} int main () {memset (head1,-1,sizeof (HEAD1)); Memset (Head,-1,sizeof (head)); scanf ("%d%d%lf", &n,&m,&k); for (int i=1;i<=m;i++) {int x,y;double z;scanf ("%d%d%lf", &x,&y,&z), add (x, y, z); Add1 (y,x,z);} Dijkstra (); Astar (1);p rintf ("%d\n", ans); return 0;}
The positive solution is to be filled with a +a* algorithm that can be persisted with the heap
K Short Circuit (* algorithm) [Usaco2008 Mar] cow run &[sdoi2010] Magic Pig Academy