B - 秋实大哥带我飞

Time Limit: 300/100MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

然而题目和题面并没有什么关系。

给出n个点,m条带权无向边,问你从1号点到n号点的最短路中有多少种走法?

Input

第一行两个数n,m分别表示点的个数和边的个数。 (2≤n≤2000,1≤m≤2000)

接下来m行,每行3个数u,v,w表示u号点到v号点有一条距离为w的边。(1≤u,v≤n,0≤w≤100000)

数据保证1号点能够到达n号点,点和边都可以被走多次。

Output

如果有无穷种走法,输出-1。否则输出走法的方案数mod 1000000009

Sample input and output

Sample Input Sample Output
4 4
1 2 1
1 3 1
2 4 1
3 4 1
2
4 4
1 2 1
1 3 1
2 4 1
3 4 0
-1

解题思路:

首先我们可以很容易得出:如果通往终点的最短路径上存在 0 边的话,那么肯定是有无穷多种走法的.

那么我们就设到达终点的状态有两种:

  1. 在通往终点的路上经过 过 0 边
  2. 在通往终点的路上没有经过 过 0 边.

这样,我们第一遍先跑一次spfa,得出两种状态的最小时间分别为t1,t2.

如果t2 < t1 ,那么路径肯定是有限条的,我们这时候再跑一次dijkstra求最短路数量即可.

那么如果t2 >= t1呢,那么最短路径上肯定存在 0 边,即显然有无穷种走法.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <set>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 2e3 + ;
const int mod = ;
const int inf = << ;
typedef struct Edge
{
int v,w;
Edge(int v,int w)
{
this->v = v ,this->w = w;
}
}; vector<Edge>E[maxn]; int n,m,ans = ,mincost[maxn][];
bool inqueue[maxn][]; typedef struct updatastatus
{
int pos;
int passzero;
updatastatus(int pos,int passzero)
{
this->pos = pos , this->passzero = passzero ;
}
}; queue<updatastatus>q; void spfa()
{
q.push(updatastatus(,));
mincost[][] = ;
mincost[][] = << ;
while(!q.empty())
{
int pos = q.front().pos , zero = q.front().passzero;q.pop();
for(int i = ; i < E[pos].size() ; ++ i)
{
int nextnode = E[pos][i].v;
int newcost = mincost[pos][zero] + E[pos][i].w;
if (zero || !E[pos][i].w)
{
if (mincost[nextnode][] == - || mincost[nextnode][] > newcost)
{
mincost[nextnode][] = newcost;
if (!inqueue[nextnode][])
{
q.push(updatastatus(nextnode,));
inqueue[nextnode][] = false;
}
}
}
else
{
if (mincost[nextnode][] == - || mincost[nextnode][] > newcost)
{
mincost[nextnode][] = newcost;
if (!inqueue[nextnode][])
{
q.push(updatastatus(nextnode,));
inqueue[nextnode][] = false;
}
}
}
}
}
} typedef struct tnode
{
int u,d;
friend bool operator < (const tnode & x,const tnode & y)
{
return x.d > y.d;
}
tnode(int u,int d)
{
this->u = u , this->d = d;
}
}; int times[maxn];
priority_queue<tnode>dq; void dijkstra()
{
bool vis[maxn];
int dis[maxn];
memset(vis,false,sizeof(vis));
for(int i = ; i <= n ; ++ i) dis[i] = inf;
dis[] = ;
times[] = ;
dq.push(tnode(,));
while(!dq.empty())
{
int u = dq.top().u , d = dq.top().d;dq.pop();
if (vis[u]) continue;
vis[u] = true;
for(int i = ; i < E[u].size() ; ++ i)
{
int nextnode = E[u][i].v;
int newcost = E[u][i].w;
if (dis[nextnode] > dis[u] + newcost)
{
dis[nextnode] = dis[u] + newcost;
times[nextnode] = times[u];
times[nextnode] %= mod;
dq.push(tnode(nextnode,dis[nextnode]));
}
else if(dis[nextnode] == dis[u] + newcost)
{
times[nextnode] = (times[nextnode] + times[u]) % mod;
}
}
}
} int main(int argc,char *argv[])
{
scanf("%d%d",&n,&m);
for(int i = ; i < m ; ++ i)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
E[u].pb(Edge(v,w));
E[v].pb(Edge(u,w));
}
memset(mincost,-,sizeof(mincost));
memset(inqueue,false,sizeof(inqueue));
memset(times,,sizeof(times));
spfa();
if (mincost[n][] <= mincost[n][] && mincost[n][] != -)
printf("-1\n");
else if (mincost[n][] != -)
{
dijkstra();
printf("%d\n",times[n] % mod);
}
else
printf("-1\n");
return ;
}

UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>的更多相关文章

  1. UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>

    I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  2. UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>

    L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>

    J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  4. UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>

    H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others) Subm ...

  5. UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>

    D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  6. UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>

    C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  7. UESTC_秋实大哥与连锁快餐店 2015 UESTC Training for Graph Theory<Problem A>

    A - 秋实大哥与连锁快餐店 Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  8. UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>

    I - 排名表 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  9. UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>

    K - 王之盛宴 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

随机推荐

  1. AsyncTask实现登录功能,上传图片,get,post

    提交成功时,从服务器端返回数据“load success” 用户名.密码正确后成功登录,并且在服务器端的文件保存目录上看到了从客户端上传的图片. 客户端代码: MainActivity.java im ...

  2. python基础学习05(核心编程第二版)部分

    # -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...

  3. 动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的

    本文转自:http://www.cnblogs.com/hnsdwhl/archive/2011/07/23/2114730.html 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态 ...

  4. Exchange Server 2010升级到Exchange Server 2013概览

  5. robots.txt网站爬虫文件设置

    目录: 什么是robots.txt robots.txt使用误区 robots.txt使用技巧 什么是robots.txt? robots.txt是搜索引擎中访问网站的时候要查看的第一个文件.Robo ...

  6. Servlet实现Session

    (1)首先看一下项目的结构 是在tomcat--webaps下的myWebSites项目 在myWebSites下有仅仅有WEB-INF目录 在WEB-INF目录中有  一下目录(在classes目录 ...

  7. Android学习笔记--服务(Service)

    1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...

  8. 【js】判断设备类型,访问相应的网站

    引入 function uaredirect(f) { try { if (document.getElementById("bdmark") != null) { return ...

  9. Javascript中String、Array常用方法介绍

    string和array作为javascript内置对象,其中许多方法无论是在开发过程中,还是在面试的时候都有机会被面试官问到,这里对经常用到的方法做一个介绍,这些方法都有过很多的实际应用场景,所以对 ...

  10. Java—Integer类

    Integer类 Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 该类提供了多个方法,能在 int 类型和 String 类型之间 ...