标签:limits medium pair rms arch scan between plm direct
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 65250 | Accepted: 16053 |
Description
Input
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
Source
[Submit] [Go Back] [Status] [Discuss]
题意
在一张无向图找一条最长路,并记录这条路上的最小边权,不知道为什么前向星会TLE,是因为多组输入的memset?
CODE
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <queue> 6 #include <vector> 7 8 using namespace std; 9 typedef pair<int,int> P; 10 typedef long long LL; 11 const int inf = 0x3f3f3f3f; 12 const int maxn = 1e3+7; 13 const int kmaxn = 1e6 + 7; 14 15 int dis[maxn]; 16 int cnt=0; 17 int n,m; 18 19 struct node 20 { 21 int to,w; 22 }; 23 24 vector <node> g[maxn]; 25 /*inline void add(int u,int v,int w) 26 { 27 edge[cnt].to=v; 28 edge[cnt].w=w; 29 edge[cnt].nxt=head[u]; 30 head[u]=cnt++; 31 }*/ 32 33 /*void add(int u,int v,int w) { 34 edge[++cnt].to=v; 35 edge[cnt].nxt=head[u]; 36 edge[cnt].w=w; 37 head[u]=cnt; 38 }*/ 39 40 void dijkstra() { 41 //memset(vis,0,sizeof(vis)); 42 memset(dis,0,sizeof(dis)); 43 priority_queue<P,vector<P>,less<P> >q; 44 dis[1]=inf; 45 q.push(make_pair(inf,1)); 46 while(!q.empty()) { 47 P p = q.top(); 48 int u=q.top().second; 49 q.pop(); 50 if(p.first < dis[u]) 51 continue; 52 int d = g[u].size(); 53 //printf("size:%d\n",d); 54 for(int i = 0; i < d; i++) { 55 node e = g[u][i]; 56 int v=e.to,z=e.w; 57 if(dis[u] > dis[v] && z > dis[v]) { 58 dis[v] = min(z,dis[u]); 59 q.push(P(dis[v],v)); 60 } 61 } 62 } 63 } 64 65 int main() 66 { 67 int T; 68 scanf("%d",&T); 69 int cnt = 0; 70 while(T--) { 71 scanf("%d %d",&n, &m); 72 //memset(head, 0,sizeof(head)); 73 //memset(edge, 0,sizeof(edge)); 74 for(int i = 0; i <= n; i++) { 75 g[i].clear(); 76 } 77 int u,v,cost; 78 for(int i=1;i<=m;i++) { 79 scanf("%d %d %d",&u, &v, &cost); 80 g[u].push_back({v,cost}); 81 g[v].push_back({u,cost}); 82 } 83 dijkstra(); 84 printf("Scenario #%d:\n%d\n\n",++cnt,dis[n]); 85 } 86 return 0; 87 }
Heavy Transportation POJ - 1797
标签:limits medium pair rms arch scan between plm direct
原文地址:https://www.cnblogs.com/orangeko/p/12289220.html