51nod 1459 spfa floyd
来源:互联网 发布:数据库课程设计报告 编辑:程序博客网 时间:2024/06/08 12:16
1459 迷宫游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
Input
第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。再接下来m行,每行3个空格分隔的整数x, y, z (0<z<=200)表示道路,表示从房间x到房间y(双向)的道路,注意,最多只有一条道路连结两个房间, 你需要的时间为z。输入保证从start到end至少有一条路径。
Output
一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
Input示例
3 2 0 21 2 30 1 101 2 11
Output示例
21 6
spfa:
#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,-1,-1,1,1};const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=5e4+6;const double EPS=1e-7;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}vector<pair<int,int> >dp[maxn];int c[maxn],d[maxn],value[maxn],inq[maxn];pair<int,int> operator + (pair<int,int>a,pair<int,int>b){ return make_pair(a.first+b.first,a.second+b.second);}inline int Scan(){ int res=0,ch,flag=0; if((ch=getchar())=='-')flag=1; else if(ch>='0' && ch<='9')res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0'; return flag ? -res : res;}int main(){ int n,m,st,ed; n=Scan(); m=Scan(); st=Scan(); ed=Scan(); for(int i=0;i<n;i++) c[i]=Scan(); for(int i=0;i<n;i++) { dp[i].clear(); inq[i]=0; d[i]=INF; value[i]=c[i]; } for(int i=0;i<m;i++) { int x,y,z; x=Scan(); y=Scan(); z=Scan(); dp[x].push_back(make_pair(y,z)); dp[y].push_back(make_pair(x,z)); } queue<int >Q; Q.push(st),d[st]=0,inq[st]=1; while(!Q.empty()) { int now=Q.front(); Q.pop(); inq[now]=0; for(int i=0;i<dp[now].size();i++) { int v=dp[now][i].first; if(d[v]>d[now]+dp[now][i].second||d[v]==d[now]+dp[now][i].second &&value[v]<value[now]+c[v]) { d[v]=d[now]+dp[now][i].second; value[v]=value[now]+c[v]; if(inq[v]==1) continue; inq[v]=1; Q.push(v); } } } cout<<d[ed]<<" "<<value[ed]<<endl;}floyd:
#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,-1,-1,1,1};const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=5e4+6;const double EPS=1e-7;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}pair<int,int >dp[505][505];int c[maxn];pair<int,int> operator + (pair<int,int>a,pair<int,int>b){ return make_pair(a.first+b.first,a.second+b.second);}inline int Scan(){ int res=0,ch,flag=0; if((ch=getchar())=='-')flag=1; else if(ch>='0' && ch<='9')res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0'; return flag ? -res : res;}int main(){ int n,m,st,ed; n=Scan(); m=Scan(); st=Scan(); ed=Scan(); for(int i=0;i<n;i++) c[i]=Scan(); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i==j) dp[i][j]=make_pair(0,-c[i]); else dp[i][j]=make_pair(1e9,0); } for(int i=0;i<m;i++) { int x,y,z; x=Scan(); y=Scan(); z=Scan(); dp[x][y]=dp[y][x]=make_pair(z,-c[x]-c[y]); } for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) { dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+make_pair(0,c[k])); } printf("%d %d\n",dp[st][ed].first,-dp[st][ed].second);}
0 0
- 51nod 1459 spfa floyd
- [51NOD] 1459 迷宫游戏 [最短路][spfa]
- 51nod 1693 水群 (spfa)
- 51nod 1693 水群(spfa最短路)
- EOJ 1270 spfa/floyd
- dijkstra+SPFA+Floyd
- 51nod 1459 迷宫问题 (Dijkstra或SPFA求最短路)
- 51nod 1445 变色DNA【最短路SPFA+思维】
- 51nod 1366 贫富差距【并查集+Floyd】
- floyd、dij、bell-man、spfa
- hdu 1217 Arbitrage Floyd || SPFA
- poj 2253 Frogger (spfa) (Floyd)
- hdu3665-Seaside(SPFA,dijkstra,floyd)
- 最短路 spfa, dijkstra, Floyd
- poj Arbitrage(Floyd||spfa)
- 最短路【dijkstra】【floyd 】【spfa】
- POJ1125 Stockbroker Grapevine SPFA或Floyd
- hdu 2544 最短路 (dijkstra/floyd/spfa)
- Ubuntu常用快捷键总结
- JAVA OOP 第二章继承
- activiti工作流获取出线(outgoingTransitions)
- MATLAB实现BP神经网络
- 实体类(VO,DO,DTO)的划分
- 51nod 1459 spfa floyd
- Linux环境下Apache配置多个虚拟主机挂载多站点同时运行
- Python+Selenium练习篇之12-获取浏览器的版本号
- Android基础—Android调用WebService
- 转载CSDN博客的方法
- 安卓回调机制
- Java大数问题
- 索引的重要性
- React Native学习中之modal