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 ≤ XN). 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?

  正向边一次最短路,反向边一次就好了。。。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int INF=10e8;
const int MaxN=; struct Node
{
int v,val; Node(int _v=,int _val=):v(_v),val(_val) {}
bool operator < (const Node &a) const
{
return val>a.val;
}
}; struct Edge
{
int v,cost; Edge(int _v=,int _cost=):v(_v),cost(_cost) {}
}; vector <Edge> E[][MaxN]; void Dijkstra(int type,int lowcost[],int n,int start)
{
priority_queue <Node> que;
Node qtemp;
int u,v,c,len; for(int i=;i<=n;++i)
{
lowcost[i]=INF;
}
lowcost[start]=; que.push(Node(start,)); while(!que.empty())
{
qtemp=que.top();
que.pop(); u=qtemp.v; len=E[type][u].size(); for(int i=;i<len;++i)
{
v=E[type][u][i].v;
c=E[type][u][i].cost; if(lowcost[u]+c<lowcost[v])
{
lowcost[v]=lowcost[u]+c;
que.push(Node(v,lowcost[v]));
}
}
}
} inline void addEdge(int type,int u,int v,int c)
{
E[type][u].push_back(Edge(v,c));
} int ans1[MaxN],ans2[MaxN];
int maxans; int main()
{
int N,M,X;
int a,b,c; while(~scanf("%d %d %d",&N,&M,&X))
{
for(int i=;i<=M;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(,a,b,c);
addEdge(,b,a,c);
} for(int i=;i<=N;++i)
ans1[i]=ans2[i]=;
maxans=-; Dijkstra(,ans1,N,X);
Dijkstra(,ans2,N,X); for(int i=;i<=N;++i)
if(ans1[i]+ans2[i]>maxans)
maxans=ans1[i]+ans2[i]; cout<<maxans<<endl;
} return ;
}

(简单) POJ 3268 Silver Cow Party,Dijkstra。的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  2. POJ 3268 Silver Cow Party (Dijkstra + 优先队列)

    题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...

  3. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  4. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  5. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  9. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

随机推荐

  1. ios字体大小适应不同屏幕

    //根据button高度来设置字体大小 CGFloat dayLabelWidth = (viewWidth-10)/7-1; cancelButton = [[UIButton alloc] ini ...

  2. oracle中的exists 和in

    有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...

  3. if __name__ == '__main__'在python中的应用

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...

  4. Gson解析数据

    package com.bwie.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputS ...

  5. UVA - 437 The Tower of Babylon(dp-最长递增子序列)

    每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...

  6. 关于this指针理解

    1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将 ...

  7. ImageLoader的使用

    显示本地图片 //ImageLoader使用前必须初始化 ImageLoader imageLoader = ImageLoader.getInstance(); imageLoader.init(I ...

  8. js实现类似于add(1)(2)(3)调用方式的方法

    群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...

  9. opentsdb

    http://blog.javachen.com/2014/01/22/all-things-opentsdb.html http://blog.csdn.net/bingjie1217/articl ...

  10. C#排列组合类

    //----------------------------------------------------------------------------- // // 算法:排列组合类 // // ...