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=0;
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 0;
}
int n,m;

int spfa(int v)
{
    queue<int> q;
    int inqueue[N],dis[N];
    memset(inqueue,0,sizeof(inqueue)),inqueue[v]=1;
    q.push(v);
    for(int i=0;i<n;i++) dis[i]=INT_MAX;
    dis[v]=0;
    int times[N];
    memset(times,0,sizeof(times)),times[v]=1;

    while(!q.empty()){
        int x=q.front();
        q.pop();
        inqueue[x]=0;
        for(int j=heads[x];j!=-1;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 0;
                    inqueue[to]=1,q.push(to);
                }
            }
        }
    }
    return 1;
}
int main(void)
{
    int i,j,t;
    int a,b,c;
    scanf("%d",&t);
    while(t--){
        int w;
        memset(heads,-1,sizeof(heads));//注意不要忘记
        scanf("%d%d%d",&n,&m,&w);
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            addedge(a-1,b-1,c);
            addedge(b-1,a-1,c);

        }
        while(w--){
            scanf("%d%d%d",&a,&b,&c);
            addedge(a-1,b-1,-c);
        }
        if(!spfa(0)) printf("YES\n");
        else printf("NO\n");
    }

}

poj 3259Wormholes (spfa最短路径)

时间: 2024-08-02 05:57:55

poj 3259Wormholes (spfa最短路径)的相关文章

poj 3159(spfa最短路径)

Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 23152   Accepted: 6234 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large b

poj 3259(bellman最短路径)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30169   Accepted: 10914 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

poj 2253 (dis最短路径)

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24979   Accepted: 8114 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her,

poj 1125 (floyed 最短路径)

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26395   Accepted: 14558 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

poj 1062 (dij最短路径)

昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35968   Accepted: 10314 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000

Invitation Cards POJ 1511 SPFA || dij + heap

http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反向建一次,跑一个最短路就好. ★.cin  cout 超时 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #

poj 3463 Sightseeing 最短路径数量

题意: 求有向图中最短路和比最短路大1的路径数量. 思路: 需要理解dijkstra算法中dis[n]数组的含义,设cnt[i]表示到点i的最短路径数量,cnt1[i]表示到点i比最短路大1的路径数量.在运行dijkstra算法的过程中每次获得最小dis[i]的时候可以对所有dis[v]+w(v,i)==dis[i]的v做如下更新cnt[i]+=cnt[v],cnt1[i]+=cnt1[v].而当所有值为某数的dis[i]计算完成时也就是对任意i,dis[i]为同一值且不再变化时,可以对这些满足

poj 3268(spfa)

http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题目做多了总会见到鬼的!!!!!! 心累,不想写这个题了. 1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <iostream> 5 #define inf 0x3f

poj Wormholes 【最短路径】【bellman_ford】

Wormholes Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wor