HDU 3986 Harry Potter and the Final Battle

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3986

题目:

Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1139    Accepted Submission(s): 359

Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.

Input
First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v, w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.

Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.

Sample Input

3
4
4
1 2 5
2 4 10
1 3 3
3 4 8
3
2
1 2 5
2 3 10
2
2
1 2 1
1 2 2

Sample Output

15
-1
2

分析与总结:

这题和
HDU 1595 find the longest of the shortest
一样。

但是有所不同,那题是没有重边的,而这题有重边,所以原来我做那题用邻接矩阵的方法不能再用了。(如果有重边的话,两点间删了一条路,还有另一条路可以用,所以不一样的)。

所以这题得用邻接表的方法来做。 用邻接表来做,其实还更简单。 和原来一样记录最短路的路径,而且还要记录这条路径是上的边。由于是用邻接表的,是用“边”来保存的,所以只需要记录下这条边在数组中的位置即可,删除的时候做个标记,对这条边不进行松弛操作即可。

代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;  

const int INF = 0x7fffffff;
const int VN = 1010;
const int EN = 50005;  

struct Edge{
    int v,next,w;
    bool used;
}E[EN*2];  

int n,m,size;
int head[VN];
int d[VN];
int pre[VN];
int edge[VN];
bool flag;
bool inq[VN];  

void init(){
    flag = true;
    size=0;
    memset(head, -1, sizeof(head));
    memset(pre, -1, sizeof(pre));
}
void addEdge(int u,int v,int w){
    E[size].v=v;
    E[size].w=w;
    E[size].used = true;
    E[size].next = head[u];
    head[u] = size++;
}  

void SPFA(int src){
    memset(inq, 0, sizeof(inq));
    for(int i=1; i<=n; ++i)d[i] = INF;
    d[src] = 0;
    queue<int>q;
    q.push(src);
    while(!q.empty()){
        int u = q.front();  q.pop();
        inq[u] = false;
        for(int e=head[u]; e!=-1; e=E[e].next)if(E[e].used){
            int tmp = d[u] + E[e].w;
            if(d[E[e].v] > tmp){
                d[E[e].v] = tmp;
                if(flag){
                    pre[E[e].v] = u;
                    edge[E[e].v] = e;
                }
                if(!inq[E[e].v]){
                    inq[E[e].v] = true;
                    q.push(E[e].v);
                }
            }
        }
    }
}  

int main(){
    int T,u,v,c;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=0; i<m; ++i){
            scanf("%d%d%d",&u,&v,&c);
            addEdge(u,v,c);
            addEdge(v,u,c);
        }
        SPFA(1);
        flag=false;
        if(d[n]==INF){
            puts("-1");
            continue;
        }
        int ans = -1;
        int u = n;
        while(pre[u]!=-1){
            E[edge[u]].used = false;
            SPFA(1);
            if(d[n]==INF){
                ans=-1; break;
            }
            if(d[n]>ans) ans=d[n];
            E[edge[u]].used = true;
            u = pre[u];
        }
        printf("%d\n", ans);
    }
    return 0;
}

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索and
, The
BETWEEN
harry potter、harry potter 英文版、harry potter电影、harry potter mobi、harry potter pdf,以便于您获取更多的相关知识。

时间: 2024-09-16 00:14:41

HDU 3986 Harry Potter and the Final Battle的相关文章

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

【HDU 4771 Stealing Harry Potter&amp;#39;s Precious】BFS+状压

2013杭州区域赛现场赛二水... 类似"胜利大逃亡"的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏.有点贪心的感觉. 由于求最短时间,BFS更快捷,但耗内存,这道题就卡在这里了... 这里记录了我几次剪枝的历史...题目要求内存上限32768KB,就差最后600KB了...但我从理论上觉得已经不能再剪了,留下的结点都是盲目式搜索必然要访问的结点

hdu 4771 Stealing Harry Potter&#039;s Precious

点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走.给定k个点,问从起点开始把这k个点走过去的最小步数. 思路:题目k的最大为4,那么我们就可以直接暴力k个点的走的顺序,然后利用bfs即可 代码: #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> usi

HDU 1546 Idiomatic Phrases Game

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1546 题目: Idiomatic Phrases Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 969    Accepted Submission(s): 300 Problem Description Tom is playi

HDU 1856

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 6073    Accepted Submission(s): 2225 Problem Description Mr Wang wants some boys to help him with a project. Because the project

php中$this、static、final、const、self的用法

  本篇文章主要分项了一下关于php类中的$this,static,final,const,self这几个关键字使用方法. $this $this表示当前实例,在类的内部方法访问未声明为const及static的属性时,使用$this->value='phpernote';的形式.常见用法如: $this->属性 $this->方法 举例如下:  代码如下   <?php class MyClass{  private $name;  public  function __cons

hdu 1527

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1527 hint:威佐夫博弈 基本类似于模板 #include <iostream> #include <cmath> #include <cstdio> using namespace std; const double q = (1 + sqrt(5.0)) / 2.0; // 黄金分割数 int Wythoff(int a, int b) { if (a > b)

hdu 2551 竹青遍野

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2551 hint:就是读懂题就行了 #include <iostream> #include <cstdio> using namespace std; typedef long long LL; LL data[1005]; int main() { data[0]=0; for(int i=1; i<1005; i++) data[i]+=data[i-1]+i*i*i; LL

hdu 2054 A == B?

http://acm.hdu.edu.cn/showproblem.php?pid=2054 此题巨坑,刚开始我以为是简单的水题,就用strcmp过, but错了,后来经过我苦思冥想,结果还有几组数据 0.0 和 0,1.000和1.0 , 但是我不太确定前面的0是不是有作用我还是写了,但是有人过的时候,前面的0没考虑比如: 002和2可能是相等的,也可能是不想等的所以不用判断,只能说明hdu数据不是很强啊,嘿嘿 代码如下: #include <iostream> #include <c