codeforces 689B Mike and Shortcuts 最短路

题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1。问1到各个点之间的最短距离。

题目思路:SPFA求最短路

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0x3f3f3f3f
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int a[MAX],vis[MAX],dist[MAX],n,k;

struct node
{
    int u,v,w,next;
}G[MAX];

void Add(int u,int v,int w)
{
    G[k].u=u;
    G[k].v=v;
    G[k].w=w;
    G[k].next=a[u];
    a[u]=k++;
}

void SPFA()
{
    queue<int>Q;
    int st=1;
    vis[1]=1;
    dist[1]=0;
    Q.push(st);
    while(!Q.empty())
    {
        st=Q.front();
        Q.pop();
        vis[st]=0;
        for(int i=a[st];i!=-1;i=G[i].next)
        {
            int v=G[i].v;
            if(dist[v] > dist[st]+G[i].w)
            {
                dist[v]=dist[st]+G[i].w;
                if(!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}

int main()
{
    int q;
    while(scanf("%d",&n)!=EOF)
    {
        k=0;
        memset(a,-1,sizeof(a));
        memset(vis,0,sizeof(vis));
        memset(dist,INF,sizeof(dist));
        for(int i=1;i<=n;i++)
        {
            Add(i,i+1,1);
            Add(i+1,i,1);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&q);
            Add(i,q,1);
        }
        SPFA();
        for(int i=1;i<=n;i++)
            printf("%d%c",dist[i],i==n?‘\n‘:‘ ‘);
    }
    return 0;
}

时间: 2024-11-05 12:34:01

codeforces 689B Mike and Shortcuts 最短路的相关文章

Codeforces 689B - Mike and Shortcuts

题意:求从1开始的到所有点的最短路. 分析:设图中的两个点u,v, 则dis[u][v] = abs(u-v) 或 dis[u][v] = 1. 其实换个角度想就可以发现每次运动消耗的能量都是1.由于每一单位时间运动消耗的能量都是1. 状态的转移有三个:1.向前走一步.2.先后走一步.3.走捷径. /************************************************ Author :DarkTong Created Time :2016/7/7 16:09:39 F

codeforces 361 B - Mike and Shortcuts

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n int

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

Codeforces 449B Jzzhu and Cities(最短路)

题目链接:Codeforces 449B Jzzhu and Cities 题目大意:Jzzhu是一个国家的总统,这个国家有N座城市,以1为首都,已经存在了M条公路,给定M条路.并且还有K条铁轨,铁轨均有首都出发,连接si,距离为yi.现在Jzzhu想要节省经费,拆除一些铁轨,问说最多能拆除多少个铁轨,要求每座城市与首都的最短距离不变. 解题思路:最短路,多加一个标记数组,每个si标记1,如果这些点的最短距离被更新,则将对应si的标记清为0,最后统计一下剩余的标记,即为不能拆除的铁轨. #inc

Codeforces 798D Mike and distribution(贪心或随机化)

题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2}$,$p_{3}$,...,$p_{m}$满足 $a_{p1}+a_{p2}+a_{p3}+...+a_{p_{m}}>a_{1}+a_{2}+a_{3}+...+a_{n}$ $b_{p1}+b_{p2}+b_{p3}+...+b_{p_{m}}>b_{1}+b_{2}+b_{3}+...+b_

CodeForces 689C Mike and Chocolate Thieves (二分)

原题: Description Bad news came to Mike's village, some thieves stole a bunch of chocolates from the local factory! Horrible! Aside from loving sweet things, thieves from this area are known to be very greedy. So after a thief takes his number of choco

codeforces 798C Mike and gcd problem

C.Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beau

【算法系列学习】codeforces D. Mike and distribution 二维贪心

http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二维的贪心我们可以先让它变成其中一维有序,这样只需要重点考虑另一维,就会简单很多. 首先,对于题目要求的选择元素之和两倍大与所有元素之和,我们可以转化为选择元素之和大于剩下的.然后我们可以将下标按照a从大到小排序.然后选择第一个,之后每两个一组,选择b大的一个,如果n是偶数再选择最后一个. 至于这样写

CodeForces 709B Checkpoints (数学,最短路)

题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 - n-1 全访问一次, 或者是2 - n 全访问一次,有一段是访问了两次,加上就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <s