POJ-3268-Silver Cow Party
来源:互联网 发布:深圳知行科技有限公司 编辑:程序博客网 时间:2024/04/30 10:24
描述:
块田中(1≤N≤1000)都有1只牛去参加盛大的母牛聚会,这个聚会被安排在X号田(1≤X ≤N)。一共有M( 1≤M≤100,000)条单行道分别连接着两块田,且通过路i需要花Ti(1≤Ti≤100)的时间。每头母牛必需参加宴会并且在宴会结束时回到自己的田地,但是每头牛都很懒而喜欢选择化是最少的一个方案。来时的路和去时的可能不一样。求每头牛要来回的最短时间。
找出来回路程最长的。输出来回最长的距离。
大意就直接复制别人的了,解法就是2次求各点到x的最短路径。只是存的时候需要存2次,因为路径是单向路径,并非无向图,所以需要特殊处理一下。这个题用SPFA比较简单,2次求后找出最大值就OK。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#define MAX 100001using namespace std;int n,m,x,e,pnt[MAX],cost[MAX],nxt[MAX],head[MAX],dist[MAX],vis[MAX];int ea,pnta[MAX],costa[MAX],nxta[MAX],heada[MAX],dista[MAX];const int inf=1<<28;void addedge(int u,int v,int c){ pnt[e]=v;nxt[e]=head[u];cost[e]=c;head[u]=e++;}void addedgea(int u,int v,int c){ pnta[ea]=v;nxta[ea]=heada[u];costa[ea]=c;heada[u]=ea++;}void SPFA(int x,int *head,int *pnt,int *cost,int *nxt,int *dist){ for(int i=0;i<=n;i++)dist[i]=inf; memset(vis,0,sizeof(vis)); dist[x]=0; queue<int> q; q.push(x); while(!q.empty()) {int u=q.front();q.pop();vis[u]=0;for(int i=head[u];i!=-1;i=nxt[i]){ int v=pnt[i]; if(dist[u]+cost[i]<dist[v]) {dist[v]=dist[u]+cost[i];if(!vis[v]){ vis[v]=1; q.push(v);} }} }}int main(){ while(scanf("%d%d%d",&n,&m,&x)!=EOF) {e=ea=0;memset(head,-1,sizeof(head));memset(heada,-1,sizeof(heada));for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); addedgea(b,a,c);} SPFA(x,head,pnt,cost,nxt,dist); SPFA(x,heada,pnta,costa,nxta,dista); int ans=0; for(int i=1;i<=n;i++)ans=max(ans,dist[i]+dista[i]); printf("%d\n",ans); } return 0;}
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268--Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- QTextEdit捕捉回车信号
- JAVA学习:高级加密标准(AES)
- read(), readline(), readlines()方法的使用
- Tomcat并发数优化的方法总结
- CRM
- POJ-3268-Silver Cow Party
- 第一部分 基本语言 第七章 函数(7.5局部对象)
- arm-linux-insight的使用
- OnCreate
- 【项目总结(一)】---视频摘要matlab的程序实现几个问题的总结
- 黑马程序员 Java基础<十>---> IO流<2>(File类,Properties,打印流,合并流)
- 简单的二级下拉菜单(转)
- android中extends 和implements的区别
- serialVersionUID作用