BZOJ1598: [Usaco2008 Mar] Cow Run

Source: Internet
Author: User

Transmission Door

K Short Circuit, the general algorithm is the use of Astar solution, first set up the reverse side run again Dij, or SPFA something. The distance from the opposite edge can be seen as $h () $ in the valuation function. Set $dist$ for the current distance, then $f (node) =dist+h (son) $, and then run Astar, according to some inexplicable things can get the first $i$ out of the stack is the first $i$ short circuit.

Bzoj 1598//by cydiater//2016.10.25#include <iostream> #include <cstring> #include <cstdlib># Include <cstdio> #include <queue> #include <map> #include <ctime> #include <cmath># Include <bitset> #include <string> #include <algorithm> #include <iomanip>using namespace std;# Define ll long long#define up (i,j,n) for (int. i=j;i<=n;i++) #define DOWN (i,j,n) for (int i=j;i>=n;i--) #define PII pair <int,int> #define MP Make_pair#define Pii priority_queue<pii,vector<pii>,greater<pii> >const int maxn=1e6+5;const int Oo=0x3f3f3f3f;inline int read () {char ch=getchar (); int x=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int N,m,k,link[maxn],undis[maxn],len=0,f[maxn],cnt[maxn],ans[maxn],dis[maxn];bool vis[MAXN];struct Edge{int y,next , V;} E[MAXN]; Pii q,q;namespace solution{inline void Insert (int x,int Y,int v) {E[++len].next=liNK[X]; Link[x]=len;e[len].y=y;e[len].v=v;} void Init () {n=read (); M=read (); K=read (); up (i,1,m) {int x=read (), Y=read (), V=read (); Insert (x,y,v); insert (Y,X,-V);}} void Pre_dijkstra () {memset (vis,0,sizeof (Vis)); Memset (Undis,10,sizeof (Undis)); undis[1]=0; Q.push (MP (undis[1],1)); Q.empty ()) {PII tmp=q.top (); Q.pop (); int node=tmp.second;if (Vis[node]) continue;vis[node]=1;for (int i=link[node];i;i=e[i].next) if (undis[e[i].y ]>undis[node]-e[i].v&&e[i].v<0) {undis[e[i].y]=undis[node]-e[i].v; Q.push (MP (UNDIS[E[I].Y],E[I].Y));}}} void Now_astar () {memset (cnt,0,sizeof (CNT)), memset (ans,-1,sizeof (ans)), Q.push (MP (Undis[n],n)), while (!q.empty ()) { PII tmp=q.top (); Q.pop (); int node=tmp.second;cnt[node]++;if (node==1) ans[cnt[node]]=tmp.first;for (int i=LINK[node]; I;i=e[i].next) if (cnt[node]<=k&&e[i].v>0) Q.push (MP (tmp.first-undis[node]+e[i].v+undis[e[i].y],e[i ].Y));}} void Slove () {Pre_dijkstra (); Now_astar ();} void output () {Up (i,1,k) printf ("%d\n", Ans[i]);}} int main () {//freopen ("input.in", "R", stdin); using namespace Solution;init (); slove (); output (); return 0;} 

BZOJ1598: [Usaco2008 Mar] Cow run

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.