zoukankan      html  css  js  c++  java
  • poj1797 最短路

    虽然不是求最短路,但是仍然是最短路题目,题意是要求1到N点的一条路径,由于每一段路都是双向的并且有承受能力,求一条路最小承受能力最大,其实就是之前POJ2253的翻版,一个求最大值最小,一个求最小值最大,于是只要修改最短路的更新条件就可以直接跑模板了

    dij:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<vector>
     4 #include<algorithm>
     5 #include<queue>
     6 #define min(a,b) a<b?a:b
     7 const int INF=0x3f3f3f3f;
     8 using namespace std;
     9 typedef pair<int,int> pii;
    10 
    11 struct cmp{
    12     bool operator()(pii a,pii b){
    13         return a.first<b.first;
    14     }
    15 };
    16 
    17 int g[1005][1005],n,m,dist[1005];
    18 
    19 void dij(int s,int p){
    20     int i;
    21     memset(dist,-1,sizeof(dist));
    22     dist[s]=INF;
    23     priority_queue<pii,vector<pii>,cmp>q;
    24     q.push(make_pair(dist[s],s));
    25     while(!q.empty()){
    26         pii u=q.top();
    27         q.pop();
    28         if(u.first<dist[u.second])continue;
    29         for(i=1;i<=n;i++){
    30             if(~g[u.second][i]){
    31                 int j=min(u.first,g[u.second][i]);
    32                 if(dist[i]==-1||dist[i]<j){
    33                     dist[i]=j;
    34                     q.push(make_pair(dist[i],i));
    35                 }
    36             }
    37         }
    38     }
    39     printf("%d
    
    ",dist[p]);
    40 }
    41 
    42 int main(){
    43     int t;
    44     while(scanf("%d",&t)!=EOF){
    45         for(int q=1;q<=t;q++){
    46             int i;
    47             memset(g,-1,sizeof(g));
    48             scanf("%d%d",&n,&m);
    49             for(i=1;i<=m;i++){
    50                 int x,y,v;
    51                 scanf("%d%d%d",&x,&y,&v);
    52                 if(g[x][y]<v){
    53                     g[x][y]=g[y][x]=v;
    54                 }
    55             }
    56             printf("Scenario #%d:
    ",q);
    57             dij(1,n);
    58         }
    59     }
    60     return 0;
    61 }
    dij

    spfa:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 const int MAXM=1000;
     6 const int INF=0x3f3f3f3f;
     7 
     8 int g[MAXM+5][MAXM+5],dist[MAXM+5],n,m;
     9 bool vis[MAXM+5];
    10 
    11 void spfa(int s,int p){
    12     int i;
    13     memset(dist,-1,sizeof(dist));
    14     dist[s]=INF;
    15     memset(vis,0,sizeof(vis));
    16     queue<int>q;
    17     q.push(s);
    18     vis[s]=1;
    19     while(!q.empty()){
    20         int u=q.front();
    21         q.pop();
    22         vis[u]=0;
    23         for(i=1;i<=n;i++){
    24             int t=dist[u]<g[u][i]?dist[u]:g[u][i];
    25             if(i!=u&&g[u][i]!=-1&&dist[i]<t){
    26                 dist[i]=t;
    27                 if(!vis[i]){
    28                     q.push(i);
    29                     vis[i]=1;
    30                 }
    31             }
    32         }
    33     }
    34     printf("%d
    
    ",dist[p]);
    35 }
    36 
    37 int main(){
    38     int t;
    39     while(scanf("%d",&t)!=EOF){
    40         for(int q=1;q<=t;q++){
    41             int i;
    42             scanf("%d%d",&n,&m);
    43             memset(g,-1,sizeof(g));
    44             for(i=1;i<=m;i++){
    45                 int a,b,v;
    46                 scanf("%d%d%d",&a,&b,&v);
    47                 if(g[a][b]<v){
    48                     g[a][b]=g[b][a]=v;
    49                 }
    50             }
    51             printf("Scenario #%d:
    ",q);
    52             spfa(1,n);
    53         }
    54     }
    55     return 0;
    56 }
    spfa
  • 相关阅读:
    ByteArrayOutputStream 与InputStream 互相转换
    Java生成和操作Excel文件
    导出excel通用模板(程序定义模板导出)
    使用Swagger2自动生成API接口文档
    The file cannot be validated as theho st "struts.apache.org" is currently unreachable.
    Ubuntu关闭显示器技巧
    Rational License Key Error 的解决办法
    Myeclipse For Blue/Spring/Professional 9.1 破解方法及注册机 JAVA开发相关软件
    没有找到suite objects.dll 因此这个应用程序未能启动
    myEclipse下如何配置Tomcat
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/4663176.html
Copyright © 2011-2022 走看看