# 暢通工程續

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45140    Accepted Submission(s): 16764

Problem Description

Input

Output

Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output
2
-1

``````<strong><span style="font-size:18px;">#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
int n, m;
struct node
{
int to, cost;
};
struct node2
{
bool friend operator < (node2 n1, node2 n2)
{
return n1.d > n2.d;
}
int id, d;
};
vector<node>g[205];
bool finish[205];
int dis[205];
void myinit()
{
memset(finish, 0, sizeof(finish));
for(int i = 0; i < 205; i  )
{
g[i].clear();
}
memset(dis, inf, sizeof(dis));
}
void dijkstra(int s)
{
priority_queue<node2> q;
dis[s] = 0;
node2 nn1;
nn1.id = s; nn1.d = dis[s];
q.push(nn1);
while(!q.empty())
{
node2 nn2 = q.top();
q.pop();
int now = nn2.id;
if(finish[now])continue;
else finish[now] = 1;
for(int i = 0; i < g[now].size(); i  )
{
if(!finish[g[now][i].to] && g[now][i].cost   dis[now] < dis[g[now][i].to])
{
dis[g[now][i].to] = g[now][i].cost   dis[now];
}
node2 nn3;
nn3.id = g[now][i].to; nn3.d = dis[g[now][i].to];
q.push(nn3);
}
}
}
int main()
{
int i, j, x, y, c, s, t;
while(scanf("%d%d", &n, &m)!=EOF)
{
myinit();
for(i = 0; i < m; i  )
{
scanf("%d%d%d", &x, &y, &c);
node n1, n2;
n1.to = x; n1.cost = c;
n2.to = y; n2.cost = c;
g[x].push_back(n2);
g[y].push_back(n1);
}
scanf("%d%d", &s, &t);
dijkstra(s);
if(dis[t] == inf)
cout<<"-1"<<endl;
else
cout<<dis[t]<<endl;
}
return 0;
}
</span></strong>``````