Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16405 | Accepted: 7503 |
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: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, 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
题意:求结点X到每个结点与每个结点到X结点距离之和的最大值。
#include <cstdio> #include <vector> #include <queue> using namespace std; const int MAXN=1005; const int INF=0x3f3f3f3f; struct Edge{ int to,w; Edge(){} Edge(int to,int w) { this->to=to; this->w=w; } bool operator>(const Edge &es) const { return w > es.w; } }; int n,m,x; vector<Edge> arc[MAXN]; int d[MAXN][MAXN],vis[MAXN]; void dijkstra(int src) { for(int i=1;i<=n;i++) { d[src][i]=INF; vis[i]=0; } priority_queue<Edge,vector<Edge>,greater<Edge> > que; que.push(Edge(src,0)); d[src][src]=0; while(!que.empty()) { Edge now=que.top();que.pop(); int u=now.to; if(vis[u]) continue; vis[u]=1; for(int i=0,size=arc[u].size();i<size;i++) { Edge e=arc[u][i]; if(d[src][e.to]>d[src][u]+e.w) { d[src][e.to]=d[src][u]+e.w; que.push(Edge(e.to,d[src][e.to])); } } } } int main() { while(scanf("%d%d%d",&n,&m,&x)!=EOF) { for(int i=1;i<=n;i++) arc[i].clear(); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); arc[u].push_back(Edge(v,w)); } for(int i=1;i<=n;i++) { dijkstra(i); } int mx=0; for(int i=1;i<=n;i++) { if(d[x][i]+d[i][x]>mx) { mx=d[x][i]+d[i][x]; } } printf("%d ",mx); } return 0; }
spfa:
/* 15653331 1340502116 3268 Accepted 4596K 344MS G++ 1087B */ #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; const int MAXN=1005; const int INF=0x3f3f3f3f; struct Edge{ int to,w; Edge(){ } Edge(int cto,int cw):to(cto),w(cw){ } }; vector<Edge> mp[MAXN]; int n,m,x; int d[MAXN][MAXN]; int vis[MAXN]; void dijkstra(int s) { for(int i=1;i<=n;i++) { d[s][i]=INF; vis[i]=0; } d[s][s]=0; queue<int> que; vis[s]=1; que.push(s); while(!que.empty()) { int now=que.front();que.pop(); vis[now]=0; for(int i=0;i<mp[now].size();i++) { Edge e=mp[now][i]; if(d[s][e.to]>d[s][now]+e.w) { d[s][e.to]=d[s][now]+e.w; if(!vis[e.to]) { que.push(e.to); vis[e.to]=1; } } } } } int main() { while(scanf("%d%d%d",&n,&m,&x)!=EOF) { for(int i=1;i<=n;i++) mp[i].clear(); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); mp[u].push_back(Edge(v,w)); } for(int i=1;i<=n;i++) dijkstra(i); int res=0; for(int i=1;i<=n;i++) { if(d[x][i]+d[i][x]>res) { res=d[x][i]+d[i][x]; } } printf("%d ",res); } return 0; }
Java版:
dijkstra
import java.util.PriorityQueue; import java.util.Scanner; class Edge implements Comparable { int to,w; Edge(){} Edge(int to,int w) { this.to=to; this.w=w; } public int compareTo(Object obj) { Edge oth=(Edge) obj; return w - oth.w; } } class List{ static final int SIZE = 10005; private Edge[] es; private int len; List() { es=new Edge[SIZE]; len=0; } void add(Edge e) { es[len++]=e; } Edge get(int index) { return es[index]; } int length() { return len; } } public class Main{ static final int MAXN=1005; static final int INF=0x3f3f3f3f; static int n,m,x; static List[] arc = new List[MAXN]; static int[][] d = new int[MAXN][MAXN]; static boolean[] vis = new boolean[MAXN]; static void dijkstra(int src) { for(int i=1;i<=n;i++) { d[src][i]=INF; vis[i]=false; } d[src][src]=0; PriorityQueue<Edge> que = new PriorityQueue<Edge>(); que.add(new Edge(src,0)); while(!que.isEmpty()) { Edge now=que.peek();que.poll(); int u=now.to; if(vis[u]) continue; vis[u]=true; for(int i=0;i<arc[u].length();i++) { Edge e=arc[u].get(i); if(d[src][e.to]>d[src][u]+e.w) { d[src][e.to]=d[src][u]+e.w; que.add(new Edge(e.to,d[src][e.to])); } } } } public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()) { n=in.nextInt(); m=in.nextInt(); x=in.nextInt(); for(int i=1;i<=n;i++) { arc[i] = new List(); } for(int i=0;i<m;i++) { int u,v,w; u=in.nextInt(); v=in.nextInt(); w=in.nextInt(); arc[u].add(new Edge(v,w)); } for(int i=1;i<=n;i++) { dijkstra(i); } int mx=-1; for(int i=1;i<=n;i++) { if(d[i][x]+d[x][i]>mx) { mx=d[i][x]+d[x][i]; } } System.out.println(mx); } } }