[kuangbin]专题四 最短路练习 Candies POJ - 3159【dijkstra】

【题目描述】
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
在幼儿园期间,flymouse是他班上的班长。偶尔,班主任给flymouse班的孩子们带来了一大袋糖果,并让flymouse分发它们。所有的孩子都非常喜欢糖果,并经常将他们获得的糖果数量与其他人获得的糖果进行比较。一个小孩A可能有这样的想法,虽然可能是另一个孩子B在某些方面比他好,因此有理由比他更应该得到更多的糖果,他应该永远不会得到少于B的一定数量的糖果不管他实际上有多少糖果,否则他会感到不满意并去找班主任抱怨flymouse的偏见。
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
snoopy当时与flymouse一起上课。 flymouse总是将他的糖果数量与snoopy的数量进行比较。他希望使数字之间的差异尽可能大,同时让每个孩子都满意。现在他刚刚从校长那里得到了另一袋糖果,他能从中获得的最大差异是什么?

【输入】
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
输入包含单个测试用例。测试用例以两行N和M分别不超过30 000和150 000的行开始。 N是班上孩子的数量,孩子们编号为1到N.snoopy和flymouse总是编号为1和N.然后按照M行依次按顺序排列三个整数A,B和c,这意味着孩子A认为小孩B应该永远不会超过他的糖果c个。

【输出】
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
输出一行只有所需的最大差异。差异保证是有限的。

【样例输入】
2 2
1 2 5
2 1 4

【样例输出】
5

【提示】
32-bit signed integer type is capable of doing all arithmetic.
32位有符号整数类型能够完成所有算术。

题目链接:https://cn.vjudge.net/problem/POJ-3159

dijkstra模板题
cin超时,scanf超时,加读入优化就过了

代码如下:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include<cctype>
using namespace std;
#define INF 0x3f3f3f3f
static const int MAXN=30000;
int read()
{
    int x=0,f=0;
    char ch=0;
    while(!isdigit(ch))
    {
        f|=ch=='-';
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return f?-x:x;
}
struct Node{
    int v,c;
    Node(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const Node &r) const {
        return c>r.c;
    }
};
struct Edge{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN+10];
bool vis[MAXN+10];
int dist[MAXN+10];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    priority_queue<Node> Q;
    while(!Q.empty()) Q.pop();
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            int cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int n,m;
    // cin>>n>>m;
    // scanf("%d%d",&n,&m);
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        // cin>>a>>b>>c;
        a=read(),b=read(),c=read();
        addedge(a,b,c);
    }
    dijkstra(n,1);
    // cout<<dist[n]<<endl;
    printf("%d\n",dist[n]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值