HDU1688(Sightseeing)
题目链接:传送门
题目大意:给你一幅图(单向边),找出从起点到终点有多少条不同路径(最短路或者比最短路长度大1)
题目思路:二维dijkstra,真的是要对dijkstra理解非常透彻才行,距离数组d开成二维表示最短路与次短路,同时需要一个cnt数组,记录有多少条路径
对于当前点有四种更新情况:
1.if(距离<最短路) 更新最短路与次短路(包括路径数)
2.if(距离==最短路) 更新最短路路径数
3.if(距离<次小) 更新次短路(包括路径数)
4.if(距离==距离) 更新次短路路径数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define seg int root,int l,int r
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
#define Min(x,y) (x<y?x:y)
#define Max(x,y) (x>y?x:y)
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 100000007
#define inf 0x3f3f3f3f
#define N 1005
#define maxn 10001000
typedef long long LL;
typedef pair<int,int> PII; int n,x,y;
int vis[N][],d[N][];
int cnt[N][];
int head[N];
struct Node{
int to,next,v;
Node(int a=,int b=,int c=){
to=a; next=b; v=c;
}
}node[N*]; int ncnt;
struct Edge{
int x,v,fl;
Edge(int a=,int b=,int c=):x(a),v(b),fl(c){}
bool operator<(const Edge &a)const{
return v>a.v;
}
}temp,edge;
inline void add(int x,int y,int v){
node[ncnt]=Node(y,head[x],v);
head[x]=ncnt++;
} void dijkstra(){
mst(vis,);
mst(d,inf);
mst(cnt,);
cnt[x][]=;
d[x][]=;
priority_queue<Edge>q;
q.push(Edge(x,,));
while(!q.empty()){
edge=q.top();q.pop();
if(vis[edge.x][edge.fl]) continue;
int s=edge.x;
int flag=edge.fl;
vis[s][flag]=;
for(int i=head[s];~i;i=node[i].next){
int e=node[i].to;
if(d[e][]>node[i].v+d[s][flag]){ ///更新最短路与次短路
if(d[e][]>d[e][]){
d[e][]=d[e][];
cnt[e][]=cnt[e][];
q.push(Edge(e,d[e][],));
}
d[e][]=node[i].v+d[s][flag];
cnt[e][]=cnt[s][flag];
q.push(Edge(e,d[e][],));
}
else if(d[e][]==node[i].v+d[s][flag]){ ///更新最短路路径数
cnt[e][]+=cnt[s][flag];
}
else if(d[e][]>node[i].v+d[s][flag]){ ///更新次短路
d[e][]=node[i].v+d[s][flag];
cnt[e][]=cnt[s][flag];
q.push(Edge(e,d[e][],));
}
else if(d[e][]==node[i].v+d[s][flag]){ ///更新次短路路径数
cnt[e][]+=cnt[s][flag];
}
}
}
} int main(){
int i,j,group,k,m,v;
scanf("%d",&group);
while(group--){
mst(head,-);
ncnt=;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d%d",&x,&y,&v);
add(x,y,v);
}
scanf("%d%d",&x,&y);
dijkstra();
int ans=cnt[y][];
if(d[y][]+==d[y][]) ans+=cnt[y][];
printf("%d\n",ans);
}
return ;
}
HDU1688(Sightseeing)的更多相关文章
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- 【POJ3621】Sightseeing Cows
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8331 Accepted: 2791 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8276 Accepted: 3489 Desc ...
- HDU 1688 Sightseeing
题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
随机推荐
- AutoConfig工具使用指南
转载:http://blog.csdn.net/fighterandknight/article/details/70245905 13.1. 需求分析 13.1.1. 解决方案 13.2. Auto ...
- java.io.IOException: Attempted read from closed stream. 异常,解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
- angular - 如何运行在起来 - 使用nginx
nginx下载地址,使用的是标准版的: 点击下载nginx nginx下载完后,解压 dist文件夹下面所有angular文件放入html文件夹中. 最后命令行cd到当前nginx.exe目录,启动命 ...
- centos7,py2和py3共存
1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...
- JAVA 模块
commons-lang3 maven repository, 项目主页 fastjson maven repository, 项目主页 fastjson 是阿里巴巴开源的序列化和反序列化 JSON ...
- 基于ShaderX5的顶点动画
关于顶点动画,ShaderX5里有篇专门来讲,在虚幻3引擎里也有一个更加丰富的实现.使用了一个Pivot Painter的3dmax脚本.其实自己灵活用vertex color可以避开使用Pivot ...
- 【分享】Nginx日志分析(上)
在很多时候,我们会非常关注网站的访问量,比如网站的日PV是多少.网站某个功能上线之后点击量是多少,像这些东西都是需要从web容器中的访问日志统计出来的,下面我们看一下如何在nginx中统计网站的访问信 ...
- 在WebService中使用Microsoft.Practices.EnterpriseLibrary.Data配置数据库
1. 新建WebApplication1项目 1.1 新建—Web—ASP.NET Empty Web Application--WebApplication1 1.2 添加一个WebForm1 2. ...
- centos6安装docker
rpm -ivh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm --import /etc ...
- iOS- 详解文本属性Attributes(转)
iOS- 详解文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont s ...