Silver Cow Party POJ - 3268 (dijkstra)

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 26103 Accepted: 11930

Description

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively:  NM, and  X 
Lines 2.. M+1: Line  i+1 describes road  i with three space-separated integers:  AiBi, and  Ti. The described road runs from farm  Ai to farm  Bi, requiring  Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

路径是单向的,利用dijkstra正反两次建图搜索,采用邻接表建图,利用优先队列。

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 9999999

using namespace std;

typedef pair<int,int> p;

int M,N,X;
int A,B,L;
int dis[1005];
int dis2[1005];
struct edge
{
    int to;
    int cost;
};
vector<edge> G[100005],G2[100005];
void Dij()
{
    priority_queue<p,vector<p>,greater<p> > que;
    fill(dis,dis+1005,INF);
    dis[X]=0;
    que.push(p(0,X));
    while(!que.empty()){
        p a=que.top();
        que.pop();
        if(a.first>dis[a.second])
            continue;
        int b=a.second;
        for(int i=0;i<G[b].size();i++){
            edge e=G[b][i];
            if(dis[e.to]>dis[b]+e.cost){
                dis[e.to]=dis[b]+e.cost;
                que.push(p(dis[e.to],e.to));
            }
        }
    }
}
void Dij2()
{
    priority_queue<p,vector<p>,greater<p> > que;
    fill(dis2,dis2+1005,INF);
    dis2[X]=0;
    que.push(p(0,X));
    while(!que.empty()){
        p a=que.top();
        que.pop();
        if(a.first>dis2[a.second])
            continue;
        int b=a.second;
        for(int i=0;i<G2[b].size();i++){
            edge e=G2[b][i];
            if(dis2[e.to]>dis2[b]+e.cost){
                dis2[e.to]=dis2[b]+e.cost;
                que.push(p(dis2[e.to],e.to));
            }
        }
    }
}
int main()
{
    cin>>N>>M>>X;
    for(int i=0;i<M;i++){
        cin>>A>>B>>L;
        edge a;
        a.to=B,a.cost=L;
        G[A].push_back(a);
        edge b;
        b.to=A,b.cost=L;
        G2[B].push_back(b);
    }
    Dij();
    Dij2();
    int num=0;
    for(int i=1;i<=N;i++)
    {
        if(num<dis[i]+dis2[i])
            num=dis[i]+dis2[i];
    }
    cout<<num<<endl;
}

利用数组建立邻接表,非常方便,而且0ms就通过了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define INF 10000000000LL

using namespace std;

int N,M,X;
typedef pair<int,int> p;
int u[100005],v[100005],w[100005];
int frist[1005],next[100005];
int dis[1005];
int d[1005];


void dji()
{
    priority_queue<p,vector<p>,greater<p> >que;
    fill(dis,dis+1005,INF);
    dis[X]=0;
    que.push(p(0,X));
    while(!que.empty()){
        p a=que.top();
        que.pop();
        int t1=a.first;
        int t2=a.second;
        if(a.first>dis[a.second])
            continue;
        int k=frist[t2];
        while(k!=-1){
            if(dis[v[k]]>dis[t2]+w[k]){
                dis[v[k]]=dis[t2]+w[k];
                que.push(p(dis[v[k]],v[k]));
            }
            k=next[k];
        }
    }
}

int main()
{
    scanf("%d%d%d",&N,&M,&X);
    fill(frist,frist+1005,-1);
    for(int i=1;i<=M;i++){
        scanf("%d%d%d",&u[i],&v[i],&w[i]);
        next[i]=frist[u[i]];
        frist[u[i]]=i;
    }
    dji();
    long long num=0;
    for(int i=1;i<=N;i++){
        d[i]+=dis[i];
    }
    fill(frist,frist+1005,-1);
    for(int i=1;i<=M;i++){
        swap(u[i],v[i]);
        next[i]=frist[u[i]];
        frist[u[i]]=i;
    }
    dji();
    for(int i=1;i<=N;i++){
        d[i]+=dis[i];
    }
    for(int i=1;i<=N;i++){
        if(num<d[i])
            num=d[i];
    }
    cout<<num;
    return 0;
}

基于SSM框架的跨境电商借卖平台.zip项目工程资源经过严格测试运行并且功能上ok,可复现复刻,拿到资料包后可实现复刻出一样的项目,本人系统开发经验充足(全栈),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助 【资源内容】:包含源码、工程文件、说明等。资源质量优质,放心下载使用!可实现复现;设计报告可借鉴此项目;该资源内项目代码都经过测试运行,功能ok 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 【提供帮助】:有任何使用上的问题欢迎随时与我联系,及时抽时间努力解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 质量优质,放心下载使用。下载后请首先打开说明文件(如有);项目工程可实现复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途,网络商品/电子资源资料具可复制性不支持退款。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值