1, HDU 1548 A strange lift :http://acm.hdu.edu.cn/showproblem.php?pid=1548

这道题可以灰常巧妙的转换为一道最短路题目,对第i层,按钮数字为button[i],则如果满足相加<=N,则把i到i+button[i]的路径长度设为1(巧妙将按钮次数转换为路径长度),同理,相减如果满足>=1,则把i到i-button[i]的路径长度设为1.则最后输出终点的最短路的长度即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int INF=0x3f3f3f3f; int n,a,b;
int map[][],vis[],dis[]; int SPFA(){
for(int i=;i<=n;i++){
dis[i]=INF;
vis[i]=;
}
queue<int> q;
while(!q.empty())
q.pop();
dis[a]=;
vis[a]=;
q.push(a);
while(!q.empty()){
int cur=q.front();
q.pop();
vis[cur]=;
for(int i=;i<=n;i++)
if(dis[i]>dis[cur]+map[cur][i]){
dis[i]=dis[cur]+map[cur][i];
if(!vis[i]){
vis[i]=;
q.push(i);
}
}
}
return dis[b];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n) && n){
scanf("%d%d",&a,&b);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
map[i][j]=INF;
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
if(i-x>=)
map[i][i-x]=;
if(i+x<=n)
map[i][i+x]=;
}
int ans=SPFA();
if(ans==INF)
ans=-;
printf("%d\n",ans);
}
return ;
}

另附BFS解法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; int n,a,b;
int k[],vis[]; struct node{
int x,step;
}; int BFS(){
queue<node> q;
while(!q.empty())
q.pop();
node cur,next;
cur.x=a, cur.step=;
vis[cur.x]=;
q.push(cur);
while(!q.empty()){
cur=q.front();
q.pop();
if(cur.x==b)
return cur.step;
next.x=cur.x-k[cur.x];
next.step=cur.step+;
if(next.x>= && next.x<=n && !vis[next.x]){
vis[next.x]=;
q.push(next);
}
next.x=cur.x+k[cur.x];
if(next.x>= && next.x<=n && !vis[next.x]){
vis[next.x]=;
q.push(next);
}
}
return -;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n) && n){
scanf("%d%d",&a,&b);
for(int i=;i<=n;i++)
scanf("%d",&k[i]);
memset(vis,,sizeof(vis));
int ans=BFS();
printf("%d\n",ans);
}
return ;
}

2,  HDU 3790 最短路径问题 : http://acm.hdu.edu.cn/showproblem.php?pid=3790

二维距离(路程,价格),优先判断路程…

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap,cost;
}edge[EM<<]; int n,m,cnt,head[VM];
int src,des,dis[VM],pay[VM],vis[VM]; void addedge(int cu,int cv,int cw,int cc){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].cost=cc;
edge[cnt].nxt=head[cu]; head[cu]=cnt++;
} void SPFA(){
for(int i=;i<=n;i++){
dis[i]=INF;
pay[i]=INF;
vis[i]=;
}
queue<int> q;
while(!q.empty())
q.pop();
dis[src]=;
pay[src]=;
vis[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap || (dis[v]==dis[u]+edge[i].cap && pay[v]>pay[u]+edge[i].cost)){ //二维距离(路程,价格),优先判断路程…
dis[v]=dis[u]+edge[i].cap;
pay[v]=pay[u]+edge[i].cost;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
if(n== && m==)
break;
cnt=;
memset(head,-,sizeof(head));
int u,v,d,p;
while(m--){
scanf("%d%d%d%d",&u,&v,&d,&p);
addedge(u,v,d,p); //因为是无向边,所以建双向图
addedge(v,u,d,p);
}
scanf("%d%d",&src,&des);
SPFA();
printf("%d %d\n",dis[des],pay[des]);
}
return ;
}

3, HDU  2066  一个人的旅行 : http://acm.hdu.edu.cn/showproblem.php?pid=2066

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[]; int T,S,D;
int cnt,head[];
int src,des,dis[],vis[]; void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
} int SPFA(){
queue<int> q;
while(!q.empty())
q.pop();
for(int i=;i<=;i++){
dis[i]=INF;
vis[i]=;
}
dis[src]=;
vis[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap){
dis[v]=dis[u]+edge[i].cap;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
return dis[des];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&T,&S,&D)){
cnt=;
memset(head,-,sizeof(head));
int u,v,w;
while(T--){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
src=, des=;
while(S--){
scanf("%d",&v);
addedge(src,v,);
}
while(D--){
scanf("%d",&u);
addedge(u,des,);
}
int ans=SPFA();
printf("%d\n",ans);
}
return ;
}

4, HDU  2112  HDU Today : http://acm.hdu.edu.cn/showproblem.php?pid=2112

trick:起点和终点有可能是同一点,坑爹呐。。。。。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue> using namespace std; const int N=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[N<<]; int all,cnt,head[N];
int src,des,dis[N],vis[N]; void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
} int SPFA(){
queue<int> q;
while(!q.empty())
q.pop();
for(int i=;i<=all;i++){
dis[i]=INF;
vis[i]=;
}
dis[src]=;
vis[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cap){
dis[v]=dis[u]+edge[i].cap;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
return dis[des];
} int main(){ //freopen("input.txt","r",stdin); int n;
char str1[],str2[];
char s[],t[];
map<string,int> mp;
while(~scanf("%d",&n) && n!=-){
cnt=;
memset(head,-,sizeof(head));
all=;
mp.clear();
scanf("%s%s",s,t);
mp[s]=all++; //起点和终点有可能是同一点
src=mp[s];
if(mp[t]==)
mp[t]=all++;
des=mp[t];
int w;
while(n--){
scanf("%s%s%d",str1,str2,&w);
if(mp[str1]==)
mp[str1]=all++;
if(mp[str2]==)
mp[str2]=all++;
addedge(mp[str1],mp[str2],w);
addedge(mp[str2],mp[str1],w);
}
int ans=SPFA();
if(ans==INF)
ans=-;
printf("%d\n",ans);
}
return ;
}

SPFA 上手题 数 枚:的更多相关文章

  1. 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)

    这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...

  2. 【NOIP模拟赛】chess 建图+spfa统计方案数

    似乎弗洛伊德和迪杰斯特拉都干不了统计方案数,spfa的话就是不断入队就好. #include <cstdio> #include <cstring> #include < ...

  3. 青岛理工ACM交流赛 J题 数格子算面积

    数格子算面积 Time Limit: 1000MS Memory limit: 262144K 题目描述 给你一个多边形(用’\’和’/’表示多边形的边),求多边形的面积. 输入  第一行两个正整数h ...

  4. UVA 558 判定负环,spfa模板题

    1.UVA 558 Wormholes 2.总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了..难道next还是特殊词吗 题意:科学家, ...

  5. 水题2枚 Codevs1464&&Codevs1472

    1472 体检  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 郑厂长不是正厂长 也不是副厂长 ...

  6. poj 2586 Y2K Accounting Bug(贪心算法,水题一枚)

    #include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...

  7. POJ 半平面交 模板题 三枚

    给出三个半平面交的裸题. 不会的上百度上谷(gu)歌(gou)一下. 毕竟学长的语文是体育老师教的.(卡格玩笑,别当真.) 这种东西明白就好,代码可以当模板. //poj1474 Video Surv ...

  8. NOIP2001-普及组复赛-第一题-数的计算

    题目描述 Description 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理: 1.不作任何处理; 2.在 ...

  9. PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚

    n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...

随机推荐

  1. Python在Windows下操作CH341DLL

    #! /usr/bin/env python #coding=utf-8 import os import time from ctypes import * class USBI2C(): ch34 ...

  2. Informatica 常用组件Lookup之九 配置未连接的查找转换

    在映射中,未连接的查找转换与管道是分开的.您可以使用 :LKP 引用限定符编写表达式以调用其它转换中的查找.未连接查找的常用用法包括: 测试表达式中某个查找的结果 基于查找结果过滤行 基于查找的结果将 ...

  3. 细聊MySQL的分区功能

    此篇主要介绍下MySQL的分区功能.我们分别从分区的概念.分区对于MySQL应用的优点.分区的类别及设置来和大家一起探讨下MySQL的分区. 什么是分区? MySQL在未启用分区功能时,数据库的单个表 ...

  4. Minimum Window Substring leetcode java

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  5. SQL2005,错误 0xc00470fe 数据流任务 产品级别对于 组件“源 - 2009_txt”(1) 而言不足

    今天在将txt文件导入MSSQL2005时,出了这个错误,到网上查了一下资料,说是因为没有安装SQL 2005 SP1的原因,所以我就下载了个. 安装后,再次导入数据,OK 没问题了.http://w ...

  6. 常见MIME类型

    Response对象通过设置ContentType使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据.  MIME类型格式:类别/子类别;参数 Co ...

  7. [Node.js] Load balancing a Http server

    Let's see how to do load balancing in Node.js. Before we start with the solution, you can do a test ...

  8. MySQL数据源在Spring中的配置

    干脆把MySQL的数据源配置也一起放这里,以备不时之需. MySQL的驱动包可以从这里 http://pan.baidu.com/s/1d02aZ 下载. 以下粗体部分是需要你根据实际情况调整的. & ...

  9. 用Navicat Premium 操作MySQL数据库

    1. 用Navicat来查看MySQL数据库        打开Navicat Premium–>[连接]–>[MySQL]–>[连接名:新建数据库的名字,此处为“本地”]:[主机: ...

  10. cordova 强制竖屏

    orentation的默认值是default 可使用的值有:default, landscape (横屏), portait (竖屏) orentation可以将设备锁定方向,不受设备旋转影响. 方案 ...