usaco butter java
来源:互联网 发布:c语言循环左移 编辑:程序博客网 时间:2024/05/17 00:52
/*ID: daniel.20LANG: JAVATASK: butter*/import java.util.*;import java.io.*;class node{ LinkedList<Integer> list; LinkedList<Integer> dis; node(){ list = new LinkedList<Integer>(); dis = new LinkedList<Integer>(); } public void add(int a,int b){ this.list.add(a); this.dis.add(b); }}public class butter { static int farm,path,cows; static int cowLocation[]; static node nodes[]; static int dist[][]; static void spfa(int source){ LinkedList<Integer> queue = new LinkedList<Integer>(); queue.add(source); boolean marked[] = new boolean[farm+1]; Arrays.fill(dist[source], 99999999); Arrays.fill(marked, false); dist[source][source] = 0; marked[source] = true; while(!queue.isEmpty()){ int u = queue.pop(); LinkedList<Integer> edge = nodes[u].list; for(int i=0;i<edge.size();i++){ int v = edge.get(i); int w = nodes[u].dis.get(i); if(dist[source][u]+w<dist[source][v]){ dist[source][v] = w+dist[source][u]; if(!marked[v]){ marked[v] = true; queue.add(v); } } } marked[u] = false; } } static void work() throws IOException{ int min = Integer.MAX_VALUE; PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("butter.out"))); dist = new int[farm+1][farm+1]; for(int i=1;i<farm+1;i++){ spfa(i); } for(int i=1;i<farm+1;i++){ int sum = 0; for(int j=0;j<cows;j++){ int location = cowLocation[j]; sum += dist[i][location]; } min = min<sum?min:sum; } System.out.println(min); out.println(min); out.close(); } public static void main(String[] args) throws Exception { Scanner cin = new Scanner(new File("butter.in")); //Scanner cin = new Scanner(System.in); cows = cin.nextInt(); farm = cin.nextInt(); path = cin.nextInt(); cowLocation = new int[cows]; for(int i=0;i<cows;i++){ cowLocation[i] = cin.nextInt(); } nodes = new node[farm+1]; for(int i=0;i<farm+1;i++) nodes[i] = new node(); for(int i=0;i<path;i++){ int from = cin.nextInt(); int to = cin.nextInt(); int w = cin.nextInt(); nodes[from].add(to, w); nodes[to].add(from,w); } work(); }}
Test 1: TEST OK [0.144 secs, 274800 KB] Test 2: TEST OK [0.115 secs, 274800 KB] Test 3: TEST OK [0.173 secs, 274800 KB] Test 4: TEST OK [0.202 secs, 274800 KB] Test 5: TEST OK [0.288 secs, 275824 KB] Test 6: TEST OK [0.360 secs, 275824 KB] Test 7: TEST OK [0.526 secs, 275824 KB] Test 8: TEST OK [0.670 secs, 275824 KB] Test 9: TEST OK [0.878 secs, 274800 KB] Test 10: TEST OK [0.799 secs, 275824 KB]题意是很明显的,最短路径. 而且是如此明显的所有点对所有点最短路径,第一反映就是floyd.
第二反应就是要超时...看了一下数据量800个点 接近10^9了,还是写了一下看,其实floyd大概也就是1.1秒就能过.
其实数据就是在卡floyd. 然后换SPFA上吧,优化过的dijkstra也能过.
很久没写图论, SPFA都不太会写了... 奇怪的是邻接矩阵的spfa居然过不去,也被卡住了,时间和floyd差不多,
邻接矩阵不应该这么慢啊,不是所有点都被松弛,用队列应该能优化时间的...不是很明白.
当然,所有点都遍历去找边是会慢,800个点呢. 但是居然和floyd一样了...
最后还是邻接表SPFA
Java随便写一点就很长啊,又是100行的代码. 不知道哪里有写的简短的java代码看,哎...
- usaco butter java
- USACO 3.2 Sweet Butter (butter)
- USACO: chap3 Sweet Butter
- usaco Sweet Butter
- [usaco] Sweet Butter
- USACO/butter 3.2.6
- USACO 3.2 Sweet Butter
- USACO 3.2.6 sweet butter
- USACO Sweet Butter 解题报告
- usaco Chapter 3 section 3.2 Sweet Butter
- USACO算法系列十三——butter
- [usaco]单源最短路径:3.3.5 Sweet Butter
- USACO section 3.2 Sweet Butter(SPFA)
- USACO Section 3.2 Sweet Butter - 还是SPFA好使..
- USACO Sweet Butter 与图的寻径算法
- dijkstra+堆优化 USACO 3.2.6 Sweet Butter
- USACO Sweet Butter, 难题,Bellman-Ford算法 + SPFA优化
- USACO--3.2Sweet Butter+推优化的Dijkstral算法
- Spring事务管理
- 求逆元的简单数论题
- FTP应答码
- WebSphere的类加载机制和故障排查
- CLisp 19:On-Lisp书中延续和多进程的代码
- usaco butter java
- Hibernate的merge与update方法的区别
- 守护进程的写法
- C++类和对象 新手学习问题小结(一)
- STM32之AFIO
- openldap自定义schema
- 步步为营学习android,步步惊喜(菜鸟级别的我跟你一起成长)-序言
- C++读写文件
- linux上使用java获取本机IP地址和MAC地址