UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>
B - 秋实大哥带我飞
Time Limit: 300/100MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)

然而题目和题面并没有什么关系。
给出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 |
2 |
4 4 |
-1 |
解题思路:
首先我们可以很容易得出:如果通往终点的最短路径上存在 0 边的话,那么肯定是有无穷多种走法的.
那么我们就设到达终点的状态有两种:
- 在通往终点的路上经过 过 0 边
- 在通往终点的路上没有经过 过 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>的更多相关文章
- UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>
I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>
L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- 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 ...
- UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>
H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others) Subm ...
- UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>
D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>
C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Su ...
- UESTC_秋实大哥与连锁快餐店 2015 UESTC Training for Graph Theory<Problem A>
A - 秋实大哥与连锁快餐店 Time Limit: 9000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ...
- UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>
I - 排名表 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>
K - 王之盛宴 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
随机推荐
- 【转】20个Java 代码生成器
From: http://www.cnblogs.com/skyme/archive/2011/12/22/2297592.html 1.1 CodeSmith 一款人气很旺国外的基于模板的dotne ...
- Best Meeting Point 解答
Question A group of two or more people wants to meet and minimize the total travel distance. You are ...
- Poj1741-Tree(树分治)
题意:找树上有多少对距离小于K的对数解析:树分治模板题,见注释 代码 #include<cstdio> #include<cstring> #include<string ...
- Java宝典(四)------Java中也存在内存泄露。
--Java中会存在内存泄露吗? --如果你想当然的以为Java里有了垃圾回收机制就不会存在内存泄露,那你就错了. Java里也会存在内存泄露! 我们慢慢来分析. 所谓内存泄露就是指一个不再被程序使用 ...
- Kafka的Producer以及Consumer远程调用问题
公司需要分布式的JMS,所以研究了Kafka,之前在本地都没有出现问题,但是在服务器上布Kafka的时候发现了消费者无法消费的问题. kafka布到一台服务器上面,由于业务原因,producer和ka ...
- java匿名内部类,多态,接口练习
1多态以及接口调用方法: public class Interface { public static void main(String[] args) { Al x = new Al(); jian ...
- 零拷贝概念 -- linux内核
零拷贝(zero-copy) 备快速网络接口的主要技术. 零拷贝技术通过降低或消除关键通信路径影响速率的操作,降低传输数据的操作系统开销和协议处理开销,从而有效提高通信性能,实现快速传输数据. 零拷贝 ...
- hdu 5071 Chat(模拟|Splay)
Chat Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Sub ...
- android studio github 项目导入问题
在github上面看到一个比较好的项目,导入出现了一些问题,记录如下: 项目演示效果如图:下载地址:https://github.com/asijack/PagerSlidingTabStrip 如果 ...
- 练习使用jquery.并将验证强度的功能加到注册页面中