比赛 防止颓废的小练习v0.3 评测结果
题目名称 道路重建 最终得分 0
用户昵称 ciyou 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2016-10-19 17:37:28
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
struct edge{
    int from,to,cost;
};
struct vertix{
    int dis,id;
    bool operator < (const vertix& c) const{
        return dis > c.dis;
    }
};
int vis[101],dis[101];
priority_queue<vertix> Q;
vector<edge> edges;
int N,M,D,A,B,map[101][101],G[101][101];
int dijkstra(int);
edge make_edge(int,int,int);
vertix make_vertix(int,int);
int main(){
    //ios::sync_with_stdio(false);
    freopen("rebuild.in","r",stdin);
    freopen("rebuild.out","w",stdout);
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(map,0x3f,sizeof(map));
    memset(G,-1,sizeof(G));
    cin>>N>>M;
    edges.clear(); //?
    for(int p=1;p<=M;p++){
        int i,j,k;
        cin>>i>>j>>k;
        map[i][j]=k;
        map[j][i]=k;
        edges.push_back(make_edge(i,j,0));
        G[i][j]=edges.size()-1;
        edges.push_back(make_edge(j,i,0));
        G[j][i]=edges.size()-1;
    }
    cin>>D;
    for(int k=1;k<=D;k++){
        int i,j;
        cin>>i>>j;
        edges[G[i][j]].cost=map[i][j];
        edges[G[j][i]].cost=map[j][i];
    }
    cin>>A>>B;
    dis[A]=0;
    Q.push(make_vertix(A,0));
    cout<<dijkstra(B);
    fclose(stdin);
    fclose(stdout);
    return 0;
}
edge make_edge(int u,int v,int w){
    edge temp;
    temp.from=u;
    temp.to=v;
    temp.cost=w;
    return temp;
}
vertix make_vertix(int id,int dis){
    vertix temp;
    temp.id=id;
    temp.dis=dis;
    return temp;
}
int dijkstra(int to){
    while(!Q.empty()){
        int p=Q.top().id;
        Q.pop();
        if(vis[p]) continue;
        else vis[p]=1;
        for(int i=1;i<=N;i++){
            if(G[p][i]!=-1){
                edge e=edges[G[p][i]];
                if(dis[e.to]>dis[p]+e.cost){
                    dis[e.to]=dis[p]+e.cost;
                    Q.push(make_vertix(e.to,dis[e.to]));
                }
            }
        }
    }
    return dis[to];
}