比赛 |
防止颓废的小练习v0.3 |
评测结果 |
AAAAAAAAA |
题目名称 |
道路重建 |
最终得分 |
100 |
用户昵称 |
农场主 |
运行时间 |
0.028 s |
代码语言 |
C++ |
内存使用 |
1.00 MiB |
提交时间 |
2016-10-19 17:17:25 |
显示代码纯文本
- #include<cstdio>
- #include<vector>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #define maxn 1000
- #define INF 1<<29
- using namespace std;
- class edge{
- public:
- int from,to,dis,w;
- };
- vector<edge> G[maxn];
- bool des[maxn][maxn]={0};
- int d[maxn]={0},inq[maxn]={0};
- int n,m,k,s,t;
- void spfa(int s,int t){
- for (int i=1;i<=n;i++){
- d[i]=INF;
- }
- d[s]=0; inq[s]=1;
- queue<int> q;
- q.push(s);
- while(!q.empty()){
- int x=q.front();q.pop(); inq[x]=0;
- for (int i=0;i<G[x].size();i++){
- edge& e=G[x][i];
- if (d[e.to]>d[e.from]+e.w){
- d[e.to]=d[e.from]+e.w;
- if (!inq[e.to]){
- inq[e.to]=1;
- q.push(e.to);
- }
- }
- }
- }
- printf("%d",d[t]);
- }
- void read(){
- scanf("%d%d",&n,&m);
- int u,v,w;
- for (int i=1;i<=m;i++){
- scanf("%d%d%d",&u,&v,&w);
- G[u].push_back((edge){u,v,w,0});
- G[v].push_back((edge){v,u,w,0});
- }
- scanf("%d",&k);
- for (int i=1;i<=k;i++){
- scanf("%d%d",&u,&v);
- des[u][v]=1;
- des[v][u]=1;
- }
- scanf("%d%d",&s,&t);
- for (int i=1;i<=n;i++){
- for (int j=0;j<G[i].size();j++){
- edge& e=G[i][j];
- if (des[e.from][e.to]==1){
- e.w=e.dis;
- }
- }
- }
- }
- int main(){
- freopen("rebuild.in","r",stdin);
- freopen("rebuild.out","w",stdout);
- read();
- spfa(s,t);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-