Gym - 101170I Iron and Coal (思维)

  给一个有向图,然后一些点有铁一些点有煤,然后问你从1走,至少花费多少(走一个没走过的点花费1,走过的点再走一次是不用花费的)可以拿到一个铁一个煤,点可以重复走。

  枚举从1节点到一个铁一个煤的交叉点即可,三遍bfs处理出dis[1,2,3][i]分别表示1号点到i的最小花费,i到铁的最小花费,i到煤的最小花费。

#include
using namespace std;
#define ls rt<<1
#define rs (rt<<1)+1
#define PI acos(-1)
#define eps 1e-8
#define ll long long
#define fuck(x) cout<<#x<<"     "< pii;
const int inf=1e8;
const int maxn=1e5+10;
int d[4][2]={1,0,-1,0,0,1,0,-1};
//int lowbit(int x){return x&-x;}
//void add(int x,int v){while(x<=n)bit[x]+=v,x+=lowbit(x);}
//int sum(int x){int ans=0;while(x>=1) ans+=bit[x],x-=lowbit(x);return ans;}
inline ll read() {
    ll s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(ll x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
int gcd(int x,int y){
    return y==0?x:gcd(y,x%y);
}

int dis[4][maxn],vis[maxn],n;
vectorg[maxn],ng[maxn];


void bfs1(){
    queueq;
    while(!q.empty()) q.pop();
    for(int i=1;i<=n+5;i++) vis[i]=0;
    q.push(1);
    vis[1]=1;
    dis[1][1]=0;
    while(!q.empty()){
        int u=q.front(),v;q.pop();
        for(int i=0;iq;
    while(!q.empty()) q.pop();
    for(int i=1;i<=n+5;i++) vis[i]=0;
    q.push(n+1);
    vis[n+1]=1;
    dis[2][n+1]=0;
    while(!q.empty()){
        int u=q.front(),v;q.pop();
        for(int i=0;iq;
    while(!q.empty()) q.pop();
    for(int i=1;i<=n+5;i++) vis[i]=0;
    q.push(n+2);
    vis[n+2]=1;
    dis[3][n+2]=0;
    while(!q.empty()){
        int u=q.front(),v;q.pop();
        for(int i=0;i

 

你可能感兴趣的:(思维题)