hdu 2363

枚举加最短路

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define N 105
#define INF 0x7f7f7f7f
struct Height{
    int low,high,dif;
}H[N*N];
struct Edge{
    int u ,val,next;
}e[N*N];
int h[N],p[N],vis[N],d[N],n;
bool cmp(Height a,Height b)
{
    return a.dif<b.dif;
}
void add(int n,int m)
{
    int i,x,y,val,cout = 1;
    for(i = 1 ; i <= n ; i++) p[i] = -1;
    while(m--)
    {
        scanf("%d %d %d",&x,&y,&val);
        e[cout].u = y;
        e[cout].val = val;
        e[cout].next = p[x];
        p[x] = cout++;
        e[cout].u = x;
        e[cout].val = val;
        e[cout].next = p[y];
        p[y] = cout++;
    }
}
int spfa(Height temp)
{
    int i;
    memset(vis,0,sizeof(vis));
    for(i = 1 ; i <= n ; i++) d[i] = INF;
    queue<int>q;
    vis[1] = 1;
    d[1] = 0;
    q.push(1);
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        if(h[cur]<temp.low||h[cur]>temp.high) continue;
        for(i = p[cur] ; i != -1 ; i = e[i].next)
        {

            if(d[e[i].u]>d[cur]+e[i].val && h[e[i].u]>=temp.low&&h[e[i].u]<=temp.high)
            {
                d[e[i].u] = d[cur] +e[i].val;
                if(!vis[e[i].u])
                {
                    vis[e[i].u] = 1;
                    q.push(e[i].u);
                }
            }
        }
        vis[cur] = 0;
    }
    return d[n];
}
int main()
{
    int t,m,x,y,val,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(i = 1 ; i <= n ; i++)
            scanf("%d",&h[i]);
        add(n,m);
        int k = 0;
        for(i = 1 ; i <= n ; i++)
        {
            for(j = i ; j <= n ; j++)
            {
                H[k].high = max(h[i],h[j]);
                H[k].low = min(h[i],h[j]);
                H[k].dif = H[k].high - H[k].low;
                k++;
            }
        }
        sort(H,H+k,cmp);
        int ans;
        for(i = 0 ; i < k ; i++)
        {
            ans = spfa(H[i]);
            if(ans != INF) break;
        }
        printf("%d %d\n",H[i].dif,ans);
    }
}

hdu 2363

时间: 2024-11-03 21:17:21

hdu 2363的相关文章

hdu 2363(枚举+最短路好题)

Cycling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1247    Accepted Submission(s): 411 Problem Description You want to cycle to a programming contest. The shortest route to the contest migh

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

hdu 2722 Here We Go(relians) Again (最短路径)

Here We Go(relians) Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 685    Accepted Submission(s): 335 Problem Description The Gorelians are a warlike race that travel the universe conquer

hdu 3191 How Many Paths Are There (次短路径数)

How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1010    Accepted Submission(s): 332 Problem Description oooccc1 is a Software Engineer who has to ride to the work place

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include