Codeforces 689b-mike and shortcuts

Source: Internet
Author: User
Tags time limit
Topic

B. Mike and shortcuts time limit/test 3 seconds memory limit per test 256 megabytes input standard input output Standa RD output

Recently, Mike is very busy with studying for exams and contests. Now it is going to chill a bit by doing some sight seeing in the city.

City consists of n intersections numbered from 1 to N. Mike starts walking in the located intersection Er 1 and goes along some sequence of intersections. Walking from intersection number I to intersection J requires |i-j| Units of energy. The total energy spent by Mike to visit a sequence of intersections P1 = 1, p2, ..., PK is equal to units.

Of course, walking would be boring if there were no shortcuts. A shortcut is a special path which allows Mike walking from one intersection to another requiring 1. There are exactly n shortcuts in Mike's city, the ith of them allows-walking from intersection I to intersection AI (i≤a I≤ai + 1) (but not in the opposite direction), thus there are exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, ..., PK then for each 1≤i < K satisfying pi + 1 = API and Api≠pi M Ike would spend only 1 of instead of |PI-PI + 1| Walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 1, p2 = ap1, p3 = AP2, ..., pk = apk-1, he spends exactly k-1 units of to Tal Energy walking around them.

Before going on he adventure, Mike asks you to find the minimum amount of energy required to reach each of the INTERSECTI ONS from his home.  Formally, for each 1≤i≤n Mike are interested in finding minimum possible total energy of some sequence P1 = 1, p2, ..., PK = I. Input

The contains an integer n (1≤n≤200)-the number of Mike's city intersection.

The second line contains n integers a1, a2, ..., an (i≤ai≤n, describing shortcuts of Mike's city, allowing to walk From intersection I-intersection AI using only 1 unit of energy. Please note this shortcuts don ' t allow walking in opposite directions (from AI to i). Output

In the only line print n integers m1, m2, ..., MN, where mi denotes the least amount of total energy required to walk from Intersection 1 to intersection I. Examples Input

3
2 2 3
Output
0 1 2 
Input
5
1 2 3 4 5
Output
0 1 2 3 4 
Input
7
4 4 4 4 7 7 7
Output
0 1 2 1 2 3 3 
Note

In the desired sequences are:

1:1; m1 = 0;

2:1, 2; m2 = 1;

3:1, 3; M3 = |3-1| = 2.

In the second sample case the sequence to any intersection 1 < I are always 1, I and Mi = |1-i|.

In the third sample Case-consider the following intersection sequences:

1:1; m1 = 0;

2:1, 2; M2 = |2-1| = 1;

3:1, 4, 3; M3 = 1 + |4-3| = 2;

4:1, 4; M4 = 1;

5:1, 4, 5; M5 = 1 + |4-5| = 2;

6:1, 4, 6; M6 = 1 + |4-6| = 3;

7:1, 4, 5, 7; M7 = 1 + |4-5| + 1 = 3.

the

N points labeled 1 to N, each point has a direction to Banlian to a point, the edge of 1, each point has a no to the edge, the weight of the difference between the label, ask 1th point to the other point of the shortest. Solution

Although many sides, but not every edge will be used, just reserve the weight of 1 edge, how to only 40w edge, with SPFA or Dijkstra can be. Code

#include <cstdio> #include <cstring> #include <queue> using namespace std; #define N 200005 struct Edge {int to,next,cost;}
E[N*4];
	struct node {int id,dis;
	friend bool Operator < (node A,node b) {return a.dis>b.dis;
}
};
Priority_queue <node> Q;
int head[n],cnt,dis[n],vis[n];
	void Addedge (int u,int v,int c) {e[cnt].cost=c;
	E[cnt].to=v;
	E[cnt].next=head[u];
head[u]=cnt++;
	int main () {int n,i,x;
	scanf ("%d", &n);
	Memset (head) (head,-1,sizeof);
	cnt=0;
		for (i=1;i<=n;i++) {scanf ("%d", &x);
		if (x!=i) Addedge (i,x,1);
			if (i!=n) {Addedge (i,i+1,1);
		Addedge (i+1,i,1);
	}} dis[1]=0;
	Node now,tmp;
	tmp.id=1;
	tmp.dis=0;
	Q.push (TMP);
	int num=0;
		while (!q.empty ()) {now=q.top ();
		Q.pop ();
		if (vis[now.id]) continue;
		Vis[now.id]=1;
		num++;
		if (num==n) break;
			for (i=head[now.id];~i;i=e[i].next) {int v=e[i].to;
			if (Vis[v]) continue; if (dis[v]==0| | Dis[now.id]+e[i].cost<dis[v]) {dis[v]=Dis[now.id]+e[i].cost;
				Tmp.id=v;
				TMP.DIS=DIS[V];
			Q.push (TMP);
	}} for (i=1;i<=n;i++) printf ("%d", dis[i]);
	Puts ("");
return 0; }


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.