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. bzoj3431 [Usaco2014 Jan]Bessie Slows Down

    Description [Brian Dean, 2014] Bessie the cow is competing in a cross-country skiing event at the wi ...

  2. Find the Celebrity 解答

    Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  3. 我的Android进阶之旅------>Android拍照小例子

    今天简单的学习了一下android拍照的简单实现. 当然该程序是个小例子,非常简单,没有什么复杂的操作,但是可以学习到Android 拍照API流程. 1.在布局文件中添加一个 surfaceView ...

  4. 《Java程序员面试笔试宝典》之 instanceof有什么作用

    instanceof是Java语言中的一个二元运算符,它的作用是判断一个引用类型的变量所指向的对象是否是一个类(或接口.抽象类.父类)的实例,即它左边的对象是否是它右边的类的实例,返回boolean类 ...

  5. 浅谈NoSQL之MongoDB数据库

    对于SQL数据库(关系型数据库)我们大家都有所了解,比如MySQL,sqlserver,oracle等数据库.在日常的开发过程中我们遇到服务器端的数据存储时几乎第一反应就是使用SQL据库像我们最常见的 ...

  6. pat 1049 Counting Ones

    要统计1到N之间‘1’的个数,如数11包含2个1.所以当N=12时,答案为5. 思想: 找规律,假设ans[N]表示1到N的‘1’的个数,则有a[100]=(a[10]-1)*9+10+a[10]-1 ...

  7. linux经常使用命令:打包、复制等

    备份文件 tar -cf /home/app20140703bak.tar /home/app/uat/test.war 拷贝文件到目标目录 例示: cp -af /app/wasapp/appnam ...

  8. oracle开启/关闭归档模式

    1.改变非归档模式到归档模式: 1)SQL> conn / as sysdba (以DBA身份连接数据库) 2)SQL> shutdown immediate;(立即关闭数据库) 3)SQ ...

  9. 面试前的准备---C#知识点回顾----04

    播下的种子,慢慢开始发芽收获了,陆陆续续offer就来了,该轮到我挑的时候了 今天面试的一家公司,技术问的相对宽广和细致,程度令人发指 1.谈谈ViewState 这个问题,回答的好,工资翻一级 基本 ...

  10. Entity Framework中datetime2 to datetime转换错误

    datetime2 to datetime 报错. 因为EF中,DATETIME类型默认是datetime2,数据库默认是datetime. 解决方案: 1.改数据库字段类型为datetime2 2. ...