POJ 0332 The Flash

传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=302

The Flash
难度级别:B; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B

试题描述

"My name is Barry Allen and I‘m the fastest man alive. When I was a child, I saw my mom killed by something impossible, and my father went to prison for her murder. Then an accident made me the impossible. To the outside world, I‘m an ordinary forensic scientist. But secretly, I used my speed to fight crim and find others like me. And one day, I‘ll find who killed my mother and get justice for my father. I am the flash."

以上是xjr背诵的闪电侠片头的片段。

B.I.T.C.H.们玩得正High时,闪电侠来得瑟他的速度了。他邀请xjr和Avengers观看他夜晚在城市跑步的场景。

他的路线是这样的:(一共有n个城市,城市由1到n编号,城市之间有m条边)

1 → 2 → 1 → 3 → 1 → 4 → 1 → 5 → 1 → … → n-1 → 1 → n → 1

但由于闪电侠速度超过闪电,他也有了一种和电流一样的心理——走最短路,现在你需要回答闪电侠最少需要走多少千米的路程。


输入

第一行,两个整数n和m(见试题描述)
接下来m行,每行三个整数a,b和c,表示从a城市到b城市有一条有向边,长度为c

输出

闪电侠走的最短路程

输入示例

5 10
2 3 8
1 5 90
3 5 82
1 2 76
1 3 8
5 3 4
4 1 23
4 5 6
3 5 6
5 4 2

输出示例

232

其他说明

1 < n, c < 1001
1 < m < 100001
数据保证城市1能到达任何城市,并且任何城市能到达城市1

题解:最短路瞎跑大水题(出题人竟然没有卡SPFA!!!)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define inf 10000000
 8 #define PAU putchar(‘ ‘)
 9 #define ENT putchar(‘\n‘)
10 using namespace std;
11 const int maxn=1000+10,maxm=100000+10;
12 struct ShortiestPath{
13     struct Tedge{int x,y,w,next;}adj[maxm];int fch[maxn],ms;
14     int d[maxn],S,n;bool inque[maxn];
15     void AddEdge(int u,int v,int w){adj[++ms]=(Tedge){u,v,w,fch[u]};fch[u]=ms;return;}
16     void init(int S,int n){
17         this->S=S;this->n=n;ms=0;
18         memset(inque,false,sizeof(inque));
19         memset(fch,0,sizeof(fch));
20         for(int i=1;i<=n;i++) d[i]=inf;
21         return;
22     }
23     void SPFA(){
24         queue<int>Q;d[S]=0;Q.push(S);
25         while(!Q.empty()){
26             int u=Q.front();Q.pop();inque[u]=false;
27             for(int i=fch[u];i;i=adj[i].next){
28                 int v=adj[i].y;
29                 if(d[v]>d[u]+adj[i].w){
30                     d[v]=d[u]+adj[i].w;
31                     if(!inque[v]){
32                         inque[v]=true;
33                         Q.push(v);
34                     }
35                 }
36             }
37         } return;
38     }
39 }p1,p2;
40 inline int read(){
41     int x=0,sig=1;char ch=getchar();
42     while(!isdigit(ch)){if(ch==‘-‘)sig=-1;ch=getchar();}
43     while(isdigit(ch))x=10*x+ch-‘0‘,ch=getchar();
44     return x*=sig;
45 }
46 inline void write(int x){
47     if(x==0){putchar(‘0‘);return;}if(x<0)putchar(‘-‘),x=-x;
48     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
49     for(int i=len-1;i>=0;i--)putchar(buf[i]+‘0‘);return;
50 }
51 int n,m;
52 void init(){
53     n=read();m=read();
54     p1.init(1,n);p2.init(1,n);
55     for(int i=1;i<=m;i++){
56         int a=read(),b=read(),c=read();
57         p1.AddEdge(a,b,c);
58         p2.AddEdge(b,a,c);
59     }
60     return;
61 }
62 void work(){
63     p1.SPFA();p2.SPFA();
64     return;
65 }
66 void print(){
67     int ans=0;
68     for(int i=1;i<=n;i++){
69         ans+=p1.d[i]+p2.d[i];
70     }
71     write(ans);
72     return;
73 }
74 int main(){init();work();print();return 0;}

当然啦还是写Dijkstra比较稳妥,小健建のDijkstra:(原谅我懒到一定境界了。。。)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=1010;
 7 const int maxm=100010;
 8 const int INF=100000000;
 9 struct Edge
10 {
11    int from,to,dist;
12 };
13 struct Heapnode
14 {
15    int d,u;
16    bool operator < (const Heapnode& rhs) const
17    {
18       return d>rhs.d;
19    }
20 };
21 struct Dijkstra
22 {
23    int n,m;
24    int p[maxn];
25    Edge edges[maxm];
26    int first[maxn],next[maxm];
27    int d[maxn];
28    bool done[maxn];
29    void init(int n)
30    {
31         this->n=n;
32         m=0;
33         memset(first,0,sizeof(first));
34    }
35    void AddEdge(int from,int to,int dist)
36    {
37         edges[++m]=(Edge){from,to,dist};
38         next[m]=first[from];
39         first[from]=m;
40    }
41    void dijkstra(int s)
42    {
43        memset(done,0,sizeof(done));
44        priority_queue<Heapnode> Q;
45        for(int i=1;i<=n;i++) d[i]=INF;
46        d[s]=0;
47        Q.push((Heapnode){0,s});
48        while(!Q.empty())
49        {
50           Heapnode x=Q.top();
51           Q.pop();
52           if(done[x.u]) continue;
53           done[x.u]=1;
54           for(int i=first[x.u];i;i=next[i])
55           {
56              Edge& v=edges[i];
57              if(d[v.to]>d[x.u]+v.dist)
58              {
59                 d[v.to]=d[x.u]+v.dist;
60                 Q.push((Heapnode){d[v.to],v.to});
61              }
62           }
63        }
64    }
65 }sol1,sol2;
66 int main()
67 {
68     int n,m,a,b,c;
69     scanf("%d%d",&n,&m);
70     sol1.init(n); sol2.init(n);
71     while(m--)
72     {
73         scanf("%d%d%d",&a,&b,&c);
74         sol1.AddEdge(a,b,c);
75         sol2.AddEdge(b,a,c);
76     }
77     sol1.dijkstra(1); sol2.dijkstra(1);
78     long long ans=0;
79     for(int i=1;i<=n;i++) ans+=sol1.d[i]+sol2.d[i];
80     printf("%lld\n",ans);
81     return 0;
82 }
时间: 2024-08-11 16:17:07

POJ 0332 The Flash的相关文章

POJ 2299 逆序对(归并排序)

终于解决了一个忧伤好久的问题,严重拖了项目进度,深感惭愧!一直被一系列的问题所困扰,然后又只能自己一个人摸索,也是一段辛酸忧伤史,现在小结一下上个月在做二维码的过程中所碰到的问题以及解决办法,现在庆幸终于解决好了,终于能将这个功能告一段落,一下小结也是分享一下Unity的某些"坑",让同行少走弯路,起码在二维码这方面应该会有所启迪,欣慰的是接下来几天终于可以做自己应该做的事情了! 效果图: 先小结一下碰到的问题: 1.Unity工程屏幕方向与Android工程屏幕方向要一致的问题 本来

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in