P1339 [USACO09OCT]熱浪Heat Wave

我太lj了,因此趁着夜色刷了道最短路的水題。。。。而後,,我炸了。 ios

  題目描述:

   The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for good eating but are not so adept at creating creamy delicious dairy products. Farmer John is leading the charge to deliver plenty of ice cold nutritious milk to Texas so the Texans will not suffer the heat too much.c++

FJ has studied the routes that can be used to move milk from Wisconsin to Texas. These routes have a total of T (1 <= T <= 2,500) towns conveniently numbered 1..T along the way (including the starting and ending towns). Each town (except the source and destination towns) is connected to at least two other towns by bidirectional roads that have some cost of traversal (owing to gasoline consumption, tolls, etc.). Consider this map of seven towns; town 5 is theide

source of the milk and town 4 is its destination (bracketed integers represent costs to traverse the route):ui

   Traversing 5-6-3-4 requires spending 3 (5->6) + 4 (6->3) + 3 (3->4) = 10 total expenses.this

Given a map of all the C (1 <= C <= 6,200) connections (described as two endpoints R1i and R2i (1 <= R1i <= T; 1 <= R2i <= T) and costs (1 <= Ci <= 1,000), find the smallest total expense to traverse from the starting town Ts (1 <= Ts <= T) to the destination town Te (1 <= Te <= T).spa

  德克薩斯純樸的民眾們這個夏天正在遭受巨大的熱浪!!!他們的德克薩斯長角牛吃起來不錯,但是他們並非很擅長生產富含奶油的乳製品。Farmer John此時以先天下之憂而憂,後天下之樂而樂的精神,一馬當先地承擔起向德克薩斯運送大量的養分冰涼的牛奶的重任,以減輕德克薩斯人忍受酷暑的痛苦。3d

   FJ已經研究過能夠把牛奶從威斯康星運送到德克薩斯州的路線。這些路線包括起始點和終點先一共通過T (1 <= T <= 2,500)個城鎮,方便地標號為1T。除了起點和終點外地每一個城鎮由兩條雙向道路連向至少兩個其它地城鎮。每條道路有一個經過費用(包括油費,過路費等等)。code

  給定一個地圖,包含C (1 <= C <= 6,200)條直接鏈接2個城鎮的道路。每條道路由道路的起點Rs,終點Re (1 <= Rs <= T; 1 <= Re <= T),和花費(1 <= Ci <= 1,000)組成。求從起始的城鎮Ts (1 <= Ts <= T)到終點的城鎮Te(1 <= Te <= T)最小的總費用。blog

輸入輸出格式:

輸入格式: ci

  第一行: 4個由空格隔開的整數: T, C, Ts, Te

  第2到第C+1行: 第i+1行描述第i條道路。有3個由空格隔開的整數: Rs, ReCi 

輸出格式:

  一個單獨的整數表示從TsTe的最小總費用。數據保證至少存在一條道路。 

輸入輸出樣例:

  輸入樣例:

 

   輸出樣例:

說明

  【樣例說明】  5->6->1->4 (3 + 1 + 3)

LCA的愛情,被一道‘熱浪’沖毀了??????

調了一個小時的熱浪還沒調出來,個人dijkstra怎麼了??????

 

#include"bits/stdc++.h"

#define maxn 11111

using namespace std;

int cnt,s,t,n,m/*,g[maxn][maxn]*/,head[maxn],dis[maxn],y,z;

bool vis[maxn];

struct edge{

         int u,v,w;

         int next;

}a[maxn];

 

/*inline int read() {

         int x=0,f=1;

         char c=getchar();

         while(c<'0'||c>'9') {

                   if(c=='-')f=-1;

                   c=getchar();

         }

         while(c>='0'&&c<='9') {

                   x=x*10+c-'0';

                   c=getchar();

         }

         return x*f;

}*/

 

void add_edge(int aa,int b,int c){

         cnt++;

         a[cnt].u=aa;

         a[cnt].v=b;

         a[cnt].w=c;

         a[cnt].next=head[aa];

         head[aa]=cnt;

}

 

/*void floyed() {

         for(int k=1; k<=n; k++) {

                   for(int i=1; i<=n; i++) {

                            if(k!=i)

                                     for(int j=1; j<=i; j++)

                                               if(i!=j&&j!=k&&g[i][j]>g[i][k]+g[k][j])

                                                        g[i][j]=g[i][k]+g[k][j];

                   }

         }

}*/

 

void dijkstra(){

         priority_queue <pair<int,int> > Q;         

         memset(dis,0x3f,sizeof(dis));

         dis[s]=0;

         Q.push(make_pair(0,s));

         while(!Q.empty()){

                   int x=Q.top().second;

                   Q.pop();

                   if(vis[x]) continue;

                   vis[x]=1;

                   for(int i=head[x];i;i=a[i].next)

                            y=a[i].v,z=a[i].w;

                            if(dis[y]>dis[x]+z)

                                     dis[y]=dis[x]+z;

                                     Q.push(make_pair(-dis[y],y));

         }

}

 

int main() {

         ios::sync_with_stdio(false);

         cin>>n>>m>>s>>t;

         //n=read(); m=read(); s=read(); t=read();

/*     for(int i=1;i<=n;i++)

                   for(int j=1;j<=n;j++)

                            g[i][j]=0x3f3f3f3f;*/

         memset(vis,0,sizeof(vis));

         for(int i=1; i<=m; i++) {

                   int x,y,z;

                   cin>>x>>y>>z;

                   //x=read();y=read();z=read();

                   add_edge(x,y,z);

                   /*g[x][y]=z;

                   g[y][x]=z;*/

         }

//      floyed();

//      cout<<g[s][t];

         dijkstra();

         cout<<dis[s];

         return 0;

}

 

迷茫之處:(這種BC錯誤真的沒臉說)

  1.不將a[i].wz表示就會顯示錯誤。

  2.輸出永遠是0(應該是個人dijkstra錯了)

  3.慘兮兮,有時候真的懷疑我是否是BC。。

 AC代碼:

  

#include"bits/stdc++.h"
#define maxn 12222
using namespace std;
int cnt=0,s,t,n,m,head[maxn],dis[maxn],y,z;
bool vis[maxn];
struct edge {
    int u,v,w;
    int next;
} a[maxn];
priority_queue <pair<int,int> > Q;

void add_edge(int aa,int b,int c) {
    cnt++;
    a[cnt].u=aa;
    a[cnt].v=b;
    a[cnt].w=c;
    a[cnt].next=head[aa];
    head[aa]=cnt;
}

void dijkstra() {
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    Q.push(make_pair(0,s));
    while(Q.size()) {
        int x=Q.top().second;
        Q.pop();
        if(vis[x]) continue;
        vis[x]=1;
        for(int i=head[x]; i; i=a[i].next) {
            y=a[i].v,z=a[i].w;
            if(dis[y]>dis[x]+z) {
                dis[y]=dis[x]+z;
                Q.push(make_pair(-dis[y],y));
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);//
    cin>>n>>m>>s>>t;
    for(int i=1; i<=m; i++) {
        int x,y,z;
        cin>>x>>y>>z;
        add_edge(x,y,z);
        add_edge(y,x,z);
    }
    dijkstra();
    cout<<dis[t];
    return 0;
}

 

**之處:

  1.括號全忘了

  2.怒刪註釋和快讀?

  3.打了個這個priority_queue <edge> Q

  4.震驚!!是人性的喪失仍是lyq的泯滅????

 

耗費一個半小時,我真的是lj

相關文章
相關標籤/搜索