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. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

  2. c++智能指针《二》 std::tr1::shared_ptr

    转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...

  3. tabbar 嵌套 navigation

    -------------- 源代码:点击打开链接 ------------------------ AppDelegate.m - (BOOL)application:(UIApplication ...

  4. Node.js-require的使用方法

    require最常用的方法 require('http') 内置模块 require('./server')  “./”表示当前路径,后面跟的是相对路径 require("../lib/se ...

  5. POJ 2010 Moo University - Financial Aid 优先队列

    题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...

  6. 第10讲- UI线程阻塞及其优化

    第10讲UI线程阻塞及其优化 .UI 阻塞demo (首先在activity_main.xml中放置两个button,分别命名为button1,button2) //首先设置一个button1用来进行 ...

  7. php随机函数

    <?php function generate_password( $length = 6 ) { // 密码字符集,可任意添加你需要的字符 // $chars = 'abcdefghijklm ...

  8. 判断包含字符String.contains

    Java String.contains()方法用法实例教程, 返回true,当且仅当此字符串包含指定的char值序列 java.lang.String.contains() 方法返回true,当且仅 ...

  9. linux学习方法之二

    相信不少想学习linux的新手们正愁不知道看什么linux学习教程好,下面小编给大家收集和整理了几点比较重要的教程,供大家学习,如需想学习更多的话,可到wdlinux学堂寻找更多教程. 安装php扩展 ...

  10. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...