POJ 4046

真不知道邀请赛的时候我是怎么把这题a掉的。。。。刚刚又搞了一下居然tle了,还tle了好多遍,想想当时能做出来真是运气,反正这题就是优化到极限,像我这种用vector建图的更是伤不起。。。想起现场赛居然有些队还是1A。。。这就是差距

#include<iostream>
#include<vector>
#include<cstdio>
#include<deque>
#include<cstring>
#define MM(name,what) memset(name,what,sizeof(name))
#define pb push_back
#define i64 long long
using namespace std;

const int maxn = 1001;
const i64 inf64 = 0x3f3f3f3f3f3f3fLL;

struct Edge
{
    int from;
    int to;
    int cost;
}zx;

vector<Edge>g[maxn];
int c[maxn];
int n,m;
i64 way[maxn][maxn];
bool inq[maxn];

void spfa(int sa)
{
    deque<int>q;
    q.clear();
    for(int i=1;i<=n;i++) inq[i] = false;
    q.pb(sa);
    inq[sa] = true;

    int now,to,cost;
    i64 temp;

    while(!q.empty())
    {
        now = q.front();
        q.pop_front();
        for(int i=0;i<g[now].size();i++)
        {
            to = g[now][i].to;
            cost = g[now][i].cost;
            if(c[to] <= c[sa])
            {
                temp = way[sa][now] + cost;
                if(temp < way[sa][to])
                {
                    way[sa][to] = temp;
                    if(!inq[to])
                    {
                        inq[to] = true;
                        if(!q.empty() && temp < way[sa][q.front()])
                        {
                            q.push_front(to);
                        }
                        else
                        {
                            q.push_back(to);
                        }
                    }
                }
            }
        }
        inq[now] = false;
    }
    return ;
}

void start()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            way[i][j] = inf64;
        }
    }
    for(int i=1;i<=n;i++)
    {
        way[i][i] = 0;
        spfa(i);
    }
    return ;
}

i64 find(int now,int to)
{
    if(now == to) return c[now];
    i64 ans = inf64;
    i64 temp;
    int cc = max(c[now],c[to]);
    for(int i=1;i<=n;i++)
    {
        if(c[i] < cc) continue;
        temp = c[i] + way[i][now] + way[i][to];
        if(temp<ans)
        {
            ans = temp;
        }
    }
    if(ans == inf64)
    {
        return -1;
    }
    else
    {
        return ans;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(!n && !m) break;

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&c[i]);
            g[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&zx.from,&zx.to,&zx.cost);
            g[zx.from].pb(zx);
            swap(zx.from,zx.to);
            g[zx.from].pb(zx);
        }

        start();

        int T;
        scanf("%d",&T);
        int now,to;
        while(T--)
        {
            scanf("%d%d",&now,&to);
            printf("%lld\n",find(now,to));
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值