sdnu1223 Tom'problem A (spfa判负环)

Description

In the future ,One day, tom feel so happy ,because he have a date with a girl,but they don't live in the same city , so tom want you help him find the fastest way to the girl's city,You should note that with the development of technology, transport can go beyond the speed of light, so the time you spend would be less than zero, but if you return to the past you can not have the date with the girl,there n(1<n<=100) city in this country and three are m(1<m<1000) roads in this country;

Tom in the first city,the girl in the city n;

Input

The fist line is m,n;

Next m lines is a,b,c (a,b is the name of city , c is the time you cost from city a to city b)

Output

The shortest time to reach the girl’s city

(if tom return to the past ,out IMPOSSIBLE!)

Sample Input

1 2
1 2 10
3 4
1 2 10
2 3 10
3 4 -5

Sample Output

10
IMPOSSIBLE!

题意模糊不清。。

目的就是判有没有负环

如果有(回到了过去)就impossible

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int inf=0x3f3f3f3f;
int dis[N],vis[N],head[N],num[N];
int n,m,tot;

struct Edge
{
    int u,v,w,next;
}edge[N];

void add(int a,int b,int c)
{
    edge[tot].u=a;
    edge[tot].v=b;
    edge[tot].w=c;
    edge[tot].next=head[a];
    head[a]=tot++;
}

bool spfa(int s)
{
    queue<int>q;
    for(int i=1;i<=n;i++)
        dis[i]=inf;
    memset(vis,false,sizeof(vis));
    memset(num,0,sizeof(num));
    q.push(s);
    dis[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int pos=q.front();
        q.pop();
        vis[pos]=false;
        num[pos]++;
        for(int i=head[pos];i!=-1;i=edge[i].next)
        {
            if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w)
            {
                dis[edge[i].v]=dis[edge[i].u]+edge[i].w;
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v]=true;
                    q.push(edge[i].v);
                    if(num[edge[i].v]>=n)
                        return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF&&n+m)
    {
        int a,b,c;
        tot=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        if(!spfa(1))
            cout<<"IMPOSSIBLE!"<<'\n';
        else
            cout<<dis[n]<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 巨人网络春招 #
11175次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# 米连集团26产品管培生项目 #
4127次浏览 198人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务