zoukankan      html  css  js  c++  java
  • poj 3268 Silver Cow Party

    题目链接:http://poj.org/problem?id=3268

    解题思路:SPFA 算法(邻接矩阵)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: poj 3268
      4 problem_name: Silver Cow Party
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 const int maxn=1005;
     58 const int INF=100000;
     59 int n,dist[maxn][2],f[maxn][maxn];
     60 bool visit[maxn];
     61 
     62 void SPFA(int k,int num){
     63     int i;
     64     for(i=1;i<=n;i++){
     65         dist[i][num]=INF;
     66         visit[i]=0;
     67     }
     68     queue<int> q;
     69     dist[k][num]=0;
     70     q.push(k);
     71     visit[k]=1;
     72     while(!q.empty()){
     73         int now=q.front();
     74         q.pop();
     75         visit[now]=0;
     76         for(i=1;i<=n;i++){
     77             if(dist[now][num]+f[now][i]<dist[i][num]){
     78                 dist[i][num]=dist[now][num]+f[now][i];
     79                 if(!visit[i]){
     80                     q.push(i);
     81                     visit[i]=1;
     82                 }
     83             }
     84         }
     85     }
     86 }
     87 ///////////////////////////////////////////////////////////////////////////
     88 
     89 int main(){
     90     std::ios::sync_with_stdio(false);
     91     //freopen("in.txt","r",stdin);
     92     //freopen("out.txt","w",stdout);
     93     ///////////////////////////////////////////////////////////////////////
     94     //Add Code:
     95     int m,x,a,b,t,i,j;
     96     while(scanf("%d%d%d",&n,&m,&x)!=EOF){
     97         for(i=1;i<=n;i++) f[i][i]=0;
     98         for(i=1;i<n;i++){
     99             for(j=i+1;j<=n;j++) f[i][j]=f[j][i]=INF;
    100         }
    101         while(m--){
    102             scanf("%d%d%d",&a,&b,&t);
    103             if(t<f[a][b]) f[a][b]=t;
    104         }
    105         SPFA(x,0);
    106         for(i=1;i<n;i++){
    107             for(j=i+1;j<=n;j++){
    108                 int temp=f[i][j];
    109                 f[i][j]=f[j][i];
    110                 f[j][i]=temp;
    111             }
    112         }
    113         SPFA(x,1);
    114         int ans=0;
    115         for(i=1;i<=n;i++){
    116             if(dist[i][0]+dist[i][1]>ans) ans=dist[i][0]+dist[i][1];
    117         }
    118         printf("%d
    ",ans);
    119     }
    120     ///////////////////////////////////////////////////////////////////////
    121     return 0;
    122 }
    123 
    124 /**************************************************************************
    125 Testcase:
    126 Input:
    127 4 8 2
    128 1 2 4
    129 1 3 2
    130 1 4 7
    131 2 1 1
    132 2 3 5
    133 3 1 2
    134 3 4 4
    135 4 2 3
    136 Output:
    137 10
    138 **************************************************************************/
  • 相关阅读:
    IHttpActionResult – new way of creating responses in ASP.NET Web API 2
    Web Api 中返回JSON的正确做法
    MVC中使用RazorPDF创建PDF
    Mac上javaweb开发环境搭建介绍----git安装及使用
    Web开发环境的搭建(mysql、maven)
    父pom.xml的详解
    pom.xml中的常用依赖包总结
    Maven+Spring+SpringMVC+Mybatis中的常见错误
    jqGrid中关于工具条中的输入框中内容的读取
    jqGrid的Fomatter用于将图片url地址转换成前端图片显示的使用
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3358921.html
Copyright © 2011-2022 走看看