bessiecomehome - codetrick
http://qingtangpaomian.iteye.com/blog/1635860
计算出所有顶点到谷仓“Z”的距离,可以使用Dijkstra算法。
http://jackneus.com/programming-archives/bessie-come-home/
https://github.com/TomConerly/Competition-Programming/blob/master/USACO/Chapter2/comehome.java
X. floyd
http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html
http://blog.csdn.net/kk303/article/details/7031119
http://qingtangpaomian.iteye.com/blog/1635860
计算出所有顶点到谷仓“Z”的距离,可以使用Dijkstra算法。
http://jackneus.com/programming-archives/bessie-come-home/
https://github.com/TomConerly/Competition-Programming/blob/master/USACO/Chapter2/comehome.java
int[] dist = new int[N]; Arrays.fill(dist,Integer.MAX_VALUE); | |
PriorityQueue<Long> pq = new PriorityQueue<Long>(); | |
pq.add((0L<<32)|(51)); | |
while(pq.size() > 0) | |
{ | |
long p = pq.poll(); | |
int at = (int)(p&0xFFFFFFFFL); | |
int cost = (int)(p>>>32); | |
if(cost >= dist[at]) continue; | |
dist[at] = cost; | |
for(int i = 0; i < N;i++) | |
{ | |
if(i==at) continue; | |
if(adj[at][i] == -1) continue; | |
if(cost + adj[at][i] < dist[i]) | |
{ | |
pq.add((((long)cost + adj[at][i])<< 32)|i); | |
} | |
} | |
} | |
int maxAt = 26; | |
for(int i = 26; i < N-1;i++) | |
{ | |
if(dist[i] == Integer.MAX_VALUE) continue; | |
if(dist[maxAt] > dist[i]) | |
{ | |
maxAt = i; | |
} | |
} | |
out.println(((char)((maxAt-26)+'A'))+" "+dist[maxAt]); |
http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html
http://blog.csdn.net/kk303/article/details/7031119
void Floyd() { int i,j,k; for (k=1;k<=52;k++) for (i=1;i<=52;i++) for (j=1;j<=52;j++) if (d[i][j]-d[i][k]>d[k][j]) d[i][j]=d[i][k]+d[k][j]; } int main() { freopen("comehome.in","r",stdin); freopen("comehome.out","w",stdout); memset(d,0x7f,sizeof(d)); scanf("%d",&p); while (p--) { i=getdata(); j=getdata(); scanf("%d",&k); if (d[i][j]>k) d[i][j]=d[j][i]=k; } Floyd(); int ans=27; for (i=27;i<52;i++) if (d[i][52]<d[ans][52]) ans=i; printf("%c %d\n",'A'+ans-26-1,d[ans][52]); return 0; }Read full article from bessiecomehome - codetrick