POJ1797 Heavy Transportation(dijkstra变形,spfa)

2023-10-06 00:42

本文主要是介绍POJ1797 Heavy Transportation(dijkstra变形,spfa),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 32753 Accepted: 8700

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

TUD Programming Contest 2004, Darmstadt, Germany

[Submit]   [Go Back]   [Status]   [Discuss]

题意:给了一个无向图,有n个城市,m条道路,给出了从一个到另一个的承载量,求从1--n的最大承载量

代码1(dijkstra):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 2010
#define inf 0x3f3f3f
using namespace std;
int n,m,maxx,u;
int map[maxnum][maxnum],dis[maxnum],vis[maxnum];//dis[n]表示从1--n的最大承载量
int main()
{int t,q=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);mem(map,0);int a,b,c;while(m--){scanf("%d%d%d",&a,&b,&c);map[a][b]=map[b][a]=c;}//dijikstrafor(int i=1; i<=n; i++)//初始化{vis[i]=0;dis[i]=map[1][i];}vis[1]=1;//标记点1已经访问过for(int i=1; i<=n-1; i++){maxx=0;for(int j=1; j<=n; j++){if(!vis[j]&&dis[j]>maxx){maxx=dis[j];u=j;}}vis[u]=1;for(int j=1; j<=n; j++)if(!vis[j]&&dis[j]<min(dis[u],map[u][j]))dis[j]=min(dis[u],map[u][j]);}printf("Scenario #%d:\n%d\n\n",q++,dis[n]);//注意输出格式}return 0;
}
代码2(spfa):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 1010
#define inf 0x3f3f3f
using namespace std;
int n,m,maxx,first[maxnum],next[100010],num;
int dis[maxnum],vis[maxnum];//dis[n]表示从1--n的最大承载量
struct node
{int u, v, w;
} G[100010];
void add_map(int u,int v,int w)//邻接表存图
{G[num].u=u,G[num].v=v,G[num].w=w;next[num]=first[u];first[u]=num++;
};
int main()
{int t,T=1;scanf("%d",&t);while(t--){int k;scanf("%d%d",&n,&m);mem(first,-1);int u,v,w;num=1;for(int i=1; i<=m; i++){scanf("%d%d%d",&u,&v,&w);add_map(u,v,w);add_map(v,u,w);//无向图,存两遍}//spfamem(dis,0);mem(vis,0);vis[1]=1;dis[1]=inf;//把初始的路标记为无穷大queue<int>q;q.push(1);while(!q.empty()){k=q.front();q.pop();vis[k]=0;for(int i=first[k]; i!=-1; i=next[i]){if(dis[G[i].v]<min(dis[k],G[i].w))//如果从1到当前点的承载量小于从1--k,与当前的第i条边的承载量的最小值{dis[G[i].v]=min(dis[k],G[i].w);if(!vis[G[i].v]){vis[G[i].v]=1;q.push(G[i].v);}}}}printf("Scenario #%d:\n%d\n\n",T++,dis[n]);}return 0;
}



#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 10000+20
#define M 1000000+10
#define LL long long
using namespace std;
int n,m,len;
struct node
{int v,w,next;
} G[M];
int first[N],dis[N],vis[N];void add(int u,int v,int w)
{G[len].v=v,G[len].w=w;G[len].next=first[u];first[u]=len++;
}
void spfa()
{for(int i=1; i<=n; i++){dis[i]=-inf;vis[i]=0;}dis[1]=inf,vis[1]=1;queue<int>q;q.push(1);while(!q.empty()){int start=q.front();q.pop();vis[start]=0;for(int i=first[start]; i!=-1; i=G[i].next){int v=G[i].v,w=G[i].w;if(dis[v]<min(dis[start],w)){dis[v]=min(dis[start],w);if(!vis[v]){q.push(v);vis[v]=1;}}}}
}
int main()
{int t,q=1;scanf("%d",&t);while(t--){mem(first,-1);scanf("%d%d",&n,&m);len=0;int u,v,w;for(int i=0; i<m; i++){scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);}spfa();printf("Scenario #%d:\n%d\n\n",q++,dis[n]);}return 0;
}


好长时间不写题,感觉脑子都不够用了,还是要经常练练啊。。



这篇关于POJ1797 Heavy Transportation(dijkstra变形,spfa)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152414

相关文章

csu(背包的变形题)

题目链接 这是一道背包的变形题目。好题呀 题意:给n个怪物,m个人,每个人的魔法消耗和魔法伤害不同,求打死所有怪物所需的魔法 #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>//#include<u>#include<map

hdu3389(阶梯博弈变形)

题意:有n个盒子,编号1----n,每个盒子内有一些小球(可以为空),选择一个盒子A,将A中的若干个球移到B中,满足条件B  < A;(A+B)%2=1;(A+B)%3=0 这是阶梯博弈的变形。 先介绍下阶梯博弈: 在一个阶梯有若干层,每层上放着一些小球,两名选手轮流选择一层上的若干(不能为0)小球从上往下移动,最后一次移动的胜出(最终状态小球都在地面上) 如上图所示,小球数目依次为

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10801(乘电梯dijkstra)

题意: 给几个电梯,电梯0 ~ n-1分别可以到达很多层楼。 换乘电梯需要60s时间。 问从0层到target层最小的时间。 解析: 将进入第0层的电梯60s也算上,最后减。 坑点是如果target为0输出0。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algori

poj 3159 (spfa差分约束最短路) poj 1201

poj 3159: 题意: 每次给出b比a多不多于c个糖果,求n最多比1多多少个糖果。 解析: 差分约束。 这个博客讲差分约束讲的比较好: http://www.cnblogs.com/void/archive/2011/08/26/2153928.html 套个spfa。 代码: #include <iostream>#include <cstdio>#i

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

poj 3169 spfa 差分约束

题意: 给n只牛,这些牛有些关系。 ml个关系:fr 与 to 牛间的距离要小于等于 cost。 md个关系:fr 与 to 牛间的距离要大于等于 cost。 隐含关系: d[ i ] <= d[ i + 1 ] 解析: 用以上关系建图,求1-n间最短路即可。 新学了一种建图的方法。。。。。。 代码: #include <iostream>#include

poj 3255 次短路(第k短路) A* + spfa 或 dijkstra

题意: 给一张无向图,求从1到n的次短路。 解析: A* + spfa 或者 dijkstra。 详解见上一题:http://blog.csdn.net/u013508213/article/details/46400189 本题,spfa中,stack超时,queue的效率最高,priority_queue次之。 代码: #include <iostream>#i

poj 2449 第k短路 A* + spfa

poj 2449: 题意: 给一张有向图,求第k短路。 解析: A* + spfa。 一下转自:http://blog.csdn.net/mbxc816/article/details/7197228 “描述一下怎样用启发式搜索来解决K短路。 首先我们知道A*的基础公式:f(x)=g(x)+h(x);对h(x)进行设计,根据定义h(x)为当前的x点到目标点t所需要的实际距