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. 在CDHtmlDialog中处理WindowClosing

    要截获window.close(),就得截获CDHtmlDialog的WindowClosing.以下是示例代码: // header DECLARE_EVENTSINK_MAP() void Win ...

  2. linux之chdir函数解析

    [lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c  /********************* ...

  3. 《Java程序员面试笔试宝典》之组合与继承有什么区别

    组合和继承是面向对象中两种代码复用的方式.组合是指在新类里面创建原有类的对象,重复利用已有类的功能.继承是面向对象的主要特性之一,它允许设计人员根据其它类的实现来定义一个类的实现.组合和继承都允许在新 ...

  4. linux下部署svn服务器

    系统Linux debian 2.6.32-5-686 先安装svn工具:apt-get install subversion,耐心等待安装完成.安装完成后svn客户端.服务器都有了. 接者建立svn ...

  5. 整个Html内容以邮件的方式发送出去(取出标签包含的用户输入信息)

    需求是一个html的调查问卷,在调查问卷完成后,将问卷页面(包括用户填写的答案)完整的发送给领导. 问题出现了 填写的时候用的是jquery赋值的方法 ,比如text文本.textrear用的是val ...

  6. pyqt 右击+指定位置点击例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys from PyQt4.QtCore impor ...

  7. Unity之Bmob云存储一

    无论我们做软件还是做游戏,少不了的就是和数据打交道,对于要保存到本地的数据,我们可以采用的载体太多了.例如:txt,Xml,Sqlite,SqlServer,Mysql等等,具体使用什么那就视情况而定 ...

  8. Oracle表、列、约束的操作

    获得有关表的信息 可以直接DESCRIBE DESC[RIBE] table_name; 可以通过数据字典 SELECT * FROM user_tables WHERE table_name =xx ...

  9. class创建单击事件

    $(function () {            $(".search-button").click(function () {                $(" ...

  10. Canvas绘图方法和图像处理方法(转)

    转自:http://javascript.ruanyifeng.com/htmlapi/canvas.html 概述 Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它 ...