hdu 1596
求边权乘积的最大值,Dijkstra修改下松弛操作即可
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
double f[maxn][maxn];
double d[maxn];
bool vis[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
scanf("%lf",&f[i][j]);
}
}
int q,S,E;
scanf("%d",&q);
while(q--){
scanf("%d%d",&S,&E);
memset(d,0,sizeof(d));
d[S] = 1;
memset(vis,0,sizeof(vis));
for(int k = 0; k < n; ++k){
double maxx = 0; int maxxx;
for(int j = 1; j <= n; ++j){
if(!vis[j] && d[j] > maxx){
maxx = d[maxxx = j];
}
}
vis[maxxx] = 1;
for(int j = 1; j <= n; ++j){
if(d[j] < d[maxxx] * f[maxxx][j]){
d[j] = d[maxxx]*f[maxxx][j];
}
}
}
if(d[E]) printf("%.3f\n",d[E]);
else printf("What a pity!\n");
}
}
return 0;
}
hdu 1548
边权为1的最短路,也可以直接BFS
#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
const int INF = 0x3f3f3f3f;
int f[maxn][maxn];
int d[maxn];
bool vis[maxn];
int dijkstra(int s, int t, int n){
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[s] = 0;
for(int i = 1; i <= n; ++i){
int minx = INF, minxx;
for(int j = 1; j <= n; ++j){
if(!vis[j] && d[j] < minx){
minx = d[minxx = j];
}
}
vis[minxx] = 1;
for(int j = 1; j <= n; ++j){
if(d[j] > d[minxx] + f[minxx][j]){
d[j] = d[minxx] + f[minxx][j];
}
}
}
return d[t];
}
int main(){
int n,A,B;
while(~scanf("%d",&n) && n){
scanf("%d%d",&A,&B);
memset(f,0x3f,sizeof(f));
int x;
for(int i = 1; i <= n; ++i){
scanf("%d",&x);
if(i-x >= 1){
f[i][i-x] = 1;
}
if(i+x <= n){
f[i][i+x] = 1;
}
}
int ans = dijkstra(A,B,n);
if(ans < INF) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
hdu 2066
建一个新的源点s0,s0到点集S中点的距离都为零
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int f[maxn][maxn];
int d[maxn];
bool vis[maxn];
int n = 1000;
int dijkstra(){
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
d[0] = 0;
for(int i = 0; i <= n; ++i){
int minx = INF, minxx;
for(int j = 0; j <= n; ++j){
if(!vis[j] && d[j] < minx){
minx = d[minxx = j];
}
}
vis[minxx] = 1;
for(int j = 0; j <= n; ++j){
d[j] = min(d[j],d[minxx] + f[minxx][j]);
}
}
}
int temp[maxn];
int main(){
int m,S,T;
while(~scanf("%d%d%d",&m,&S,&T)){
memset(f,0x3f,sizeof(f));
int u,v,w;
for(int i = 0; i < m; ++i){
scanf("%d%d%d",&u,&v,&w);
if(w < f[u][v]) f[u][v] = f[v][u] = w;
}
int x;
for(int i = 0; i < S; ++i){
scanf("%d",&x);
f[0][x] = f[x][0] = 0;
}
for(int i = 0; i < T; ++i){
scanf("%d",&temp[i]);
}
dijkstra();
int minx = INF;
for(int i = 0; i < T; ++i){
if(d[temp[i]] < minx) minx = d[temp[i]];
}
printf("%d\n",minx);
}
return 0;
}
hdu 2112
利用map记录点的对应的字符串地名,然后floyd
#include<bits/stdc++.h>
using namespace std;
const int maxn = 155;
const int INF = 0x3f3f3f3f;
int f[maxn][maxn];
int n,m;
void floyd(){
for(int k = 0; k < n; ++k){
for(int i = 0 ; i < n; ++i){
for(int j = 0; j < n; ++j){
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
}
map<string,int>cnt;
int main(){
while(~scanf("%d",&m)){
if(m == -1) break;
cnt.clear();
n = 0;
string Start, End;
cin>>Start>>End;
cnt[Start] = n++;
cnt[End] = n++;
memset(f,0x3f,sizeof(f));
for(int i = 0; i < m; ++i){
string s1,s2;
int w;
cin>>s1>>s2>>w;
if(!cnt.count(s1)) cnt[s1] = n++;
if(!cnt.count(s2)) cnt[s2] = n++;
if(w < f[cnt[s1]][cnt[s2]]) f[cnt[s1]][cnt[s2]] = f[cnt[s2]][cnt[s1]] = w;
}
if(Start == End){
printf("0\n");
continue;
}
floyd();
if(f[cnt[Start]][cnt[End]] < INF) printf("%d\n",f[cnt[Start]][cnt[End]]);
else printf("-1\n");
}
return 0;
}