poj 3259Wormholes (spfa最短路径)
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<queue>
using namespace std;
#define N 5505
#define M 55000//注意边和点集的数组大小
struct edge
{
int to,value,next;
}edges[M];
int heads[N],len=;
int addedge(int u,int v,int w)
{
edges[len].to=v,edges[len].value=w,edges[len].next=heads[u];
heads[u]=len++;
return ;
}
int n,m; int spfa(int v)
{
queue<int> q;
int inqueue[N],dis[N];
memset(inqueue,,sizeof(inqueue)),inqueue[v]=;
q.push(v);
for(int i=;i<n;i++) dis[i]=INT_MAX;
dis[v]=;
int times[N];
memset(times,,sizeof(times)),times[v]=; while(!q.empty()){
int x=q.front();
q.pop();
inqueue[x]=;
for(int j=heads[x];j!=-;j=edges[j].next){
int to=edges[j].to,value=edges[j].value;
if(value+dis[x]<dis[to]){
dis[to]=value+dis[x];
if(!inqueue[to]){ //注意已经在队列里面的不用再加入队列
if(++times[to]>=n) return ;
inqueue[to]=,q.push(to);
}
}
}
}
return ;
}
int main(void)
{
int i,j,t;
int a,b,c;
scanf("%d",&t);
while(t--){
int w;
memset(heads,-,sizeof(heads));//注意不要忘记
scanf("%d%d%d",&n,&m,&w);
while(m--){
scanf("%d%d%d",&a,&b,&c);
addedge(a-,b-,c);
addedge(b-,a-,c); }
while(w--){
scanf("%d%d%d",&a,&b,&c);
addedge(a-,b-,-c);
}
if(!spfa()) printf("YES\n");
else printf("NO\n");
} }
poj 3259Wormholes (spfa最短路径)的更多相关文章
- poj 3259-- Wormholes(SPFA)
...
- poj 1511(spfa)
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
- poj 3268(spfa)
http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- poj 1511(SPFA+邻接表)
题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...
- spfa最短路径
C++代码 #include <iostream> #include <deque> #include <stack> #include <vector> ...
- poj 1932 XYZZY (最短路径)
XYZZY Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3105 Accepted: 887 Description ...
- Invitation Cards POJ 1511 SPFA || dij + heap
http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...
- POJ - 3255 SPFA+邻接表求次短路径
题意:给出m条边 , n个顶点,u [ i ]到v [ i ] 的距离w [ i ],求除了最短路的那条最短的边的长度. 思路:之前有做过相似的题,使用迪杰斯特拉算法求单源最短路径,并且记录路径,枚举 ...
随机推荐
- Bash debug
Debugging bash scripts Bash can help us to find problems in bash scripts in some ways. You don't exp ...
- C,C++经典(程序、错误程序)
1,程序 未执行完错误的return 0
- /dev/null &
java -cp .:ojdbc14.jar com.eucalyptus.dataguard.DBCheck dadifilm slbcheck Aa7788123 > /dev/null & ...
- bootstrap 响应式布局 居中问题
大家能够通过table来设置居中: display: table; width: auto; margin-left: auto; margin-right: auto;
- Extjs 4 生成饼状图的例子
前台: //远程抄表设备下落图表数据 var Store1 = new Ext.data.Store({ <span style="white-space:pre"> ...
- SQL serve创建与调用存储过程
(1)创建 2编写存储过程(创建传参的存储过程)存储过程语法网络上很多不在累述 语法解析 Use Person 指定在那个数据库下建立存储过程 if (object_id('MyFunction', ...
- WPF属性与特性的映射(TypeConverter)
1,定义一个类 public class Human { public string Name { get; set; } public Human Child { get; set; } } 2在X ...
- linux grep 指定字符串的正则表达式
cat all_uuid_log | grep "[a-z0-9]\{32\}"
- YII与Ace Admin 的集成
目录 一. 前言... 1 二.为什么要使用YII+ace. 1 三.新建YII模块... 1 四.如何修改模板... 3 五.注意的地方... 4 六.整合的不足之处... 4 一. 前言 yii- ...
- MVC定义路由
标准路由配置 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Defa ...