poj3463 Sightseeing(最短路计数+次短路计数)
来源:互联网 发布:windows桌面管理工具 编辑:程序博客网 时间:2024/06/05 08:38
求最短路的个数和比最短路大1的路的个数。可以写堆优化Dijstra,统计最短路个数和次短路个数,然后判断次短路是否是比最短路大1即可。松弛时讨论一下最小和次小的更新。
#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define pa pair<int,int>#define N 1010#define M 10010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int tst,n,m,h[N],num0=0,dis[N],dis1[N],num[N],num1[N];//dis1-次短路bool inq[N<<1];struct edge{ int to,next,val;}data[M];void Dijstra(int s){ priority_queue<pa,vector<pa>,greater<pa> >q; memset(inq,0,sizeof(inq));memset(dis,0x3f,sizeof(dis)); dis[s]=0;num[s]=1;q.push(make_pair(dis[s],s)); while(!q.empty()){ int x=q.top().second,w=q.top().first,cnt;q.pop(); if(inq[x]) continue;inq[x]=1;if(x>n) x-=n,cnt=num1[x];else cnt=num[x]; for(int i=h[x];i;i=data[i].next){ int y=data[i].to; if(dis[y]>w+data[i].val){ dis1[y]=dis[y];num1[y]=num[y]; dis[y]=w+data[i].val;num[y]=cnt; q.push(make_pair(dis[y],y)); q.push(make_pair(dis1[y],y+n));continue; }if(dis[y]==w+data[i].val){ num[y]+=cnt;continue; }if(dis1[y]>w+data[i].val){ dis1[y]=w+data[i].val;num1[y]=cnt; q.push(make_pair(dis1[y],y+n));continue; }if(dis1[y]==w+data[i].val){ num1[y]+=cnt;continue; } } }}int main(){// freopen("a.in","r",stdin); tst=read(); while(tst--){ n=read();m=read();memset(h,0,sizeof(h));num0=0; while(m--){ int x=read(),y=read(),val=read(); data[++num0].to=y;data[num0].next=h[x];h[x]=num0;data[num0].val=val; }int s=read(),t=read();Dijstra(s);if(dis1[t]-1==dis[t]) num[t]+=num1[t]; printf("%d\n",num[t]); }return 0;}
阅读全文
0 0
- poj3463 Sightseeing(最短路计数+次短路计数)
- poj3463 Sightseeing(最短路,次短路计数)
- 1688 Sightseeing(最短路+次短路+计数)
- POJ 3463 Sightseeing(最短路次短路计数)
- 【poj3463】Sightseeing(次短路-----每条边经过不止一次计数)
- poj 3463 Sightseeing 最短路和次短路计数
- poj3463 Sightseeing --- dij最短路和次短路
- POJ3463 Sightseeing(dijkstra求最短路+次短路)
- poj 3463 Sightseeing (dij 求最短路和次短路并计数)
- poj3463 最短路+次短路
- [次短路][路径计数](模板题) poj3436 Sightseeing
- poj3463(次短路)
- poj 3463 Sightseeing(最短路+次短路)
- poj 3463 Sightseeing(最短路&&次短路)
- Sightseeing(最短路与次短路之和)
- poj 3463 Sightseeing(最短路和次短路)
- hdu 1688 Sightseeing(最短路+次短路条数)
- poj 3463 Sightseeing(最短路次短路)
- 测试人眼中的车联网(比亚迪云服务-3.2.1)——车辆定位、寻车
- 实战一、电力窃漏用户自动识别
- 股票数据定向爬虫
- Ansible playbook简单示例
- elasticsearch调优经验
- poj3463 Sightseeing(最短路计数+次短路计数)
- websocket的应用场景
- Java Web后端--入职技能任务单(Mysql5.7安装和数据准备)四
- table隔行变色
- Java NIO Selector
- 【WEB】JS常见错误与处理错误策略
- 按键检测
- 使用NanoHTTPD在Android上建立本地服务器
- Eclipse 发布 JAR